Hi

I'm using the following code to append a text file:

Dim sw As IO.TextWriter
sw = IO.File.AppendText("C:\MyTextFile.txt")
sw.WriteLine(text1.text)
sw.Flush()
sw.Close()

For some reason this is the output:

frh001wa2555655
f
fr
frh
frh0
frh00
frh001
frh001w
frh001wa
frh001wa2
frh001wa26
frh001wa261
frh001wa2613
frh001wa26135
frh001wa261351
frh001wa2613519

Please help!

Thanks

Tony

Recommended Answers

All 9 Replies

Are you saying that you have a bunch of lines that added in succesion instead of one line?
Try using the StreamWriter class. I have used it on a number of occassions to write text to a file and I have had no problems.

Cheers!
Richard

Hi Richard

Thanks for your help. I've tried using the StreamWriter class and it creates the file and writes to it with no problem. I haven't figured out how to append to it using the StreamWriter class. Using ohter methods as stated in my initial post, the outcome was as you've stated:
"Are you saying that you have a bunch of lines that added in succesion instead of one line?
"
Let me eplain to you what I need to do. I have an app that I put together to perform remote tasks. I want to be able to type in the host name in a textbox, save it to a text file, and append the text file every time I type in a new host name only if the host name doesn't already exist.

Regards,

Tony

Hi,

OK, then u need to parse the whole text file..

Open the TextFile, read the Contents, and check if the Hotsname is already present... else add..
check this code:

Dim sw As System.IO.TextReader
sw = System.IO.File.OpenText("C:\MyTextFile.txt") 
Dim MyContents As String = sw.ReadToEnd 
sw.Close()
If Instr(MyContents,Text1.Text) = 0  Then
   ' Text does not contain the name
  sw = IO.File.AppendText("C:\MyTextFile.txt") 
  sw.WriteLine(text1.text)
  sw.Flush()
  sw.Close()
 
End If

Regards
Veena

This should work.

Dim sw as StreamWriter
Try
   sw = File.AppendText(txtFileName.Text)
   sw.Write(txtFilename.Text)
   sw.Flush()
Catch exc As Exception
  Msgbox(exc.Message)

Finally
  If Not sw Is Nothing Then
                sw.Close()
  EndIf
EndTry

HI all

Thank you for your help. The coed that you've all provided me with works but for some reason my output to the text file is still the same as you will notice below:

frh001wa2555655
f
fr
frh
frh0
frh00
frh001
frh001w
frh001wa
frh001wa2
frh001wa26
frh001wa261
frh001wa2613
frh001wa26135
frh001wa261351
frh001wa2613519

Tony

Hi,

Have u tried my code..?

Regards
Veena

Hi Veena

Unfortunately it didn't work I get the following error:
System.IO.streamwriter cannot be converted to System.IO.TextWriter.

Here's wahat i am trying to accomplish:

The code works fine for the first line. It's when I try to append the text file I get the funky output. Here is the actual code:

Dim sw As New System.IO.StreamWriter("D:\host.txt", True)
sw.WriteLine(Me.TextBox1.Text)
sw.Close()

What I'm trying to do is create the text file and append the file with machine names from textbox1.

the ideal output would be

frh001wa2555655
edi002ma2341234
etc...
etc...

Also,I would like to append with the host name only if the name doesn't already exist in the text file.

Thanks

Tony

Hi Veena

It turns out that the code you've provided was helpful. I've just used the StreamReader class instead.

Thanks

Code:

Dim sw As StreamWriter
Dim sr As System.IO.StreamReader
If File.Exists("D:\HostFile.txt") Then
sr = System.IO.File.OpenText("D:\HostFile.txt")
Dim MyContents As String = sr.ReadToEnd
sr.Close()
If InStr(MyContents, t_HostName.Text) = 0 Then

sw = New StreamWriter("D:\Hostfile.txt", True) 'True for appending
sw.WriteLine(Me.t_HostName.Text)
sw.Flush()
sw.Close()
End If
Else

'Pass the file path and the file name to the StreamWriter constructor.
sw = New StreamWriter("D:\Hostfile.txt", True) 'True for appending
sw.WriteLine(Me.t_HostName.Text)
'Close the file.
sw.Flush()
sw.Close()
End If

Hi,

I just didnt realise that.. Yes u will have to use StreamWriter, to write the file..

Any way, good, ur problem is solved..

Regards
Veena

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.