Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

StreamReader and StreamWriter

Status
Not open for further replies.

ceyhorn

Programmer
Nov 20, 2003
93
US
I am creating a text file that stores a list of file names so that I know what files have been processed. I have figured out how to write a file to the .txt file, but have run into two problems. 1) each time i write the file name it just overwrites the previously inserted name and 2) how can i search the file names to see if a file name exists in the file?

Here is my code for the StreamWriter
Code:
 Public Sub WriteTextFile(ByVal FileName As String)
        Dim objStreamWriter As StreamWriter

                objStreamWriter = New StreamWriter("c:\textfile.txt")

               objStreamWriter.WriteLine(FileName + " " + Date.Now)

              objStreamWriter.Close()

    End Sub
Thanks in advance,
Chris
 
Just a suggestion, but you may want to try using a dataset and XML instead. you can manually create a data set and fill it with information in code (such as when a file is scanned) then the dataset has a .WriteXML option, makes for super easy saving. And then you can use the dataset.table.select method to search the records for your desired information.

-Rick

----------------------
 
RiverGuy,

Thanks for the link, but the code on that page does not give an example of how to open the same file and add lines. I also am trying to figure out how to search to see if a filename has been added.

Chris
 
Search if a file name has been added? I see now, you are writing a string which is a file name. I thought you wanted to know if the file in which you are creating exists.

You should read up on all of the I/O namespaces.

You have methods such as .AppendText(), .ReadLine(), etc. There's lots of tutorials in MSDN for this.


 
Ok, I figured out how to add to the end of the file:
Code:
Public Sub WriteTextFile(ByVal FileName As String)
        Dim objStreamWriter As StreamWriter

                objStreamWriter = New StreamWriter("c:\textfile.txt",[COLOR=red]True[/color])

               objStreamWriter.WriteLine(FileName + " " + Date.Now)

              objStreamWriter.Close()

    End Sub

I just had to set the append option to true so that it would add the text to the end of the file.

Does anyone have an idea for searching the text file for a file name that has already been written to it?

Thanks again,

chris
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top