| | |
Help with appending text file
Please support our VB.NET advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Aug 2007
Posts: 20
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Aug 2007
Posts: 20
Reputation:
Solved Threads: 0
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
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:
Regards
Veena
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:
VB.NET Syntax (Toggle Plain Text)
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
Last edited by QVeen72; Aug 8th, 2007 at 4:10 pm.
This should work.
vb Syntax (Toggle Plain Text)
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
There is just two ways to live your life.
One is as though nothing is a miracle.
The other is as if everything is.
One is as though nothing is a miracle.
The other is as if everything is.
•
•
Join Date: Aug 2007
Posts: 20
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Aug 2007
Posts: 20
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Aug 2007
Posts: 20
Reputation:
Solved Threads: 0
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
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
![]() |
Similar Threads
- connect to text file database (Visual Basic 4 / 5 / 6)
- Appending to a text file (C)
- Output in the text file (C)
- Create stats from a text file (Java)
- Store Bluetooth remote address to a text file (C++)
- 10 line text file (Java)
- Read and write to an ASCII Text file (Java)
Other Threads in the VB.NET Forum
- Previous Thread: How do you switch between forms in vb.net?
- Next Thread: Creating Time Delays in my code
| Thread Tools | Search this Thread |
.net .net2008 30minutes 2005 2008 access account arithmetic array basic bing button buttons center check code combobox component connectionstring crystalreport data database databasesearch datagrid datagridview date design dissertation dissertations dropdownlist excel fade file-dialog filter folder ftp generatetags google gridview hardcopy images input insert intel internet mobile monitor ms net networking objects output panel passingparameters peertopeervideostreaming picturebox picturebox1 port position print printing problem problemwithinstallation project save searchbox searchvb.net select serial shutdown soap survey table tcp temperature text textbox timer timespan toolbox trim update updown user vb vb.net vb.netcode vb.netformclosing()eventpictureboxmessagebox vb2008 vbnet view visual visualbasic visualbasic.net visualstudio visualstudio2008 web winforms wpf year





