Help with appending text file

Please support our VB.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2007
Posts: 20
Reputation: tonyf is an unknown quantity at this point 
Solved Threads: 0
tonyf tonyf is offline Offline
Newbie Poster

Help with appending text file

 
0
  #1
Aug 8th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 7
Reputation: richerTextBox is an unknown quantity at this point 
Solved Threads: 0
richerTextBox richerTextBox is offline Offline
Newbie Poster

Re: Help with appending text file

 
0
  #2
Aug 8th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 20
Reputation: tonyf is an unknown quantity at this point 
Solved Threads: 0
tonyf tonyf is offline Offline
Newbie Poster

Re: Help with appending text file

 
0
  #3
Aug 8th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 848
Reputation: QVeen72 is on a distinguished road 
Solved Threads: 120
QVeen72's Avatar
QVeen72 QVeen72 is offline Offline
Practically a Posting Shark

Re: Help with appending text file

 
0
  #4
Aug 8th, 2007
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:

  1. Dim sw As System.IO.TextReader
  2. sw = System.IO.File.OpenText("C:\MyTextFile.txt")
  3. Dim MyContents As String = sw.ReadToEnd
  4. sw.Close()
  5. If Instr(MyContents,Text1.Text) = 0 Then
  6. ' Text does not contain the name
  7. sw = IO.File.AppendText("C:\MyTextFile.txt")
  8. sw.WriteLine(text1.text)
  9. sw.Flush()
  10. sw.Close()
  11.  
  12. End If

Regards
Veena
Last edited by QVeen72; Aug 8th, 2007 at 4:10 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 812
Reputation: arjunsasidharan is on a distinguished road 
Solved Threads: 13
arjunsasidharan's Avatar
arjunsasidharan arjunsasidharan is offline Offline
Practically a Posting Shark

Re: Help with appending text file

 
0
  #5
Aug 9th, 2007
This should work.

  1. Dim sw as StreamWriter
  2. Try
  3. sw = File.AppendText(txtFileName.Text)
  4. sw.Write(txtFilename.Text)
  5. sw.Flush()
  6. Catch exc As Exception
  7. Msgbox(exc.Message)
  8.  
  9. Finally
  10. If Not sw Is Nothing Then
  11. sw.Close()
  12. EndIf
  13. 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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 20
Reputation: tonyf is an unknown quantity at this point 
Solved Threads: 0
tonyf tonyf is offline Offline
Newbie Poster

Re: Help with appending text file

 
0
  #6
Aug 10th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 848
Reputation: QVeen72 is on a distinguished road 
Solved Threads: 120
QVeen72's Avatar
QVeen72 QVeen72 is offline Offline
Practically a Posting Shark

Re: Help with appending text file

 
0
  #7
Aug 12th, 2007
Hi,

Have u tried my code..?

Regards
Veena
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 20
Reputation: tonyf is an unknown quantity at this point 
Solved Threads: 0
tonyf tonyf is offline Offline
Newbie Poster

Re: Help with appending text file

 
0
  #8
Aug 13th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 20
Reputation: tonyf is an unknown quantity at this point 
Solved Threads: 0
tonyf tonyf is offline Offline
Newbie Poster

Re: Help with appending text file

 
0
  #9
Aug 15th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 848
Reputation: QVeen72 is on a distinguished road 
Solved Threads: 120
QVeen72's Avatar
QVeen72 QVeen72 is offline Offline
Practically a Posting Shark

Re: Help with appending text file

 
0
  #10
Aug 17th, 2007
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC