<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
  <channel>
    <title>'Read from and write to a text file in vb.net' Thread RSS Feed</title>
    <link>http://www.programmersheaven.com/</link>
    <description>Contains the latest posts from the thread 'Read from and write to a text file in vb.net' posted on the 'VB.NET' forum at Programmer's Heaven.</description>
    <language>en</language>
    <copyright>Copyright 2013 Programmers Heaven</copyright>
    <pubDate>Sat, 18 May 2013 22:58:31 -0700</pubDate>
    <lastBuildDate>Sat, 18 May 2013 22:58:31 -0700</lastBuildDate>
    <generator>Argotic Syndication Framework 2007.3.0.1, http://www.codeplex.com/Argotic</generator>
    <docs>http://www.rssboard.org/rss-specification</docs>
    <ttl>360</ttl>
    <image>
      <url>http://www.programmersheaven.com/images/ph.gif</url>
      <title>Programmers Heaven</title>
      <link>http://www.programmersheaven.com/</link>
      <width>88</width>
      <height>31</height>
    </image>
    <item>
      <title>Read from and write to a text file in vb.net</title>
      <link>http://www.programmersheaven.com/mb/VBNET/267668/267668/read-from-and-write-to-a-text-file-in-vbnet/</link>
      <description>&lt;strong&gt;&lt;span style="color: Red;"&gt;This message was edited by soweyoung at  2004-7-23 8:10:53&lt;/span&gt;&lt;/strong&gt;&lt;hr /&gt;&lt;br /&gt;
Hello everyone,&lt;br /&gt;
I am working on a small program in vb.net which reads a line at a time from a text file and writes a new line back to the file.&lt;br /&gt;
&lt;br /&gt;
What I am trying to do is that I read from a text file line by line and want to be able to write a new line at the every third line.&lt;br /&gt;
The code is below. Any comments would be appreciated.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click&lt;br /&gt;
        Dim inputString As String&lt;br /&gt;
        Dim sb As New StreamReader("C:\test.txt")&lt;br /&gt;
        Dim i As Long&lt;br /&gt;
&lt;br /&gt;
        i = 1&lt;br /&gt;
        inputString = sb.ReadLine&lt;br /&gt;
&lt;br /&gt;
        While Not inputString Is Nothing&lt;br /&gt;
            If i = 3 Then&lt;br /&gt;
                'want to write a line of message to a file right at this position&lt;br /&gt;
                i = 0&lt;br /&gt;
            End If&lt;br /&gt;
&lt;br /&gt;
            i += 1&lt;br /&gt;
            inputString = sb.ReadLine&lt;br /&gt;
        End While&lt;br /&gt;
    End Sub&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/VBNET/267668/267668/read-from-and-write-to-a-text-file-in-vbnet/</guid>
      <pubDate>Fri, 23 Jul 2004 08:07:16 -0700</pubDate>
      <category>VB.NET</category>
    </item>
    <item>
      <title>Re: Read from and write to a text file in vb.net</title>
      <link>http://www.programmersheaven.com/mb/VBNET/267668/267850/re-read-from-and-write-to-a-text-file-in-vbnet/#267850</link>
      <description>&lt;strong&gt;&lt;span style="color: Red;"&gt;This message was edited by kainsworth at  2004-7-25 14:8:18&lt;/span&gt;&lt;/strong&gt;&lt;hr /&gt;&lt;br /&gt;
There are many ways of doing this.  Here is just one approach - I prefer to read from file into an array or collection, make any amendments and then rewrite the file to reflect the amendments.&lt;br /&gt;
  One version of code that will achieve this is as follows:&lt;br /&gt;
&lt;pre class="sourcecode"&gt;
'  At top of Form code
Imports System.IO
Imports System.Collections.Specialized

'  The following code to be fired from an appropriate event:
        Dim MyFileText As New StringCollection  ' A collection
        Dim Fstrm As FileStream = New FileStream("MyTextFile.txt", FileMode.Open)
        '  Instantiate the StreamReader and pass it the FileStream 
        Dim StrmR As StreamReader = New StreamReader(Fstrm)
        '  Read the whole file into the collection
        While StrmR.Peek &amp;lt;&amp;gt; -1
            MyFileText.Add(StrmR.ReadLine())
        End While
        '   close the file.  
        Fstrm.Close()

        '  Edit the collection (every 3rd line)
        Dim i As Integer
        For i = 2 To MyFileText.Count - 1 Step 3
            MyFileText.Item(i) = "Replaced in Code"
        Next
        
        '  Kill the original file, then recreate it
        File.Delete("MyTextFile.txt")
        Dim FS As FileStream = New FileStream("MyTextFile.txt", FileMode.CreateNew)
        '  Write amended data block back to recreated file
        Dim sw As StreamWriter = New StreamWriter(FS)
        For i = 0 To MyFileText.Count - 1
            sw.WriteLine(MyFileText.Item(i).ToString)
        Next
        sw.Close()
        Fstrm.Close()
&lt;/pre&gt;&lt;br /&gt;
   The above will not be everybody's preferred route, and some may think the StringCollection to be overkill, but it works for me.  &lt;img src="http://www.programmersheaven.com/images/Community/smile.gif" width="15" height="15" alt="" /&gt;  If you add a decent level of exception handling to the core code, it is a fairly robust way of going about it.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/VBNET/267668/267850/re-read-from-and-write-to-a-text-file-in-vbnet/#267850</guid>
      <pubDate>Sun, 25 Jul 2004 14:05:47 -0700</pubDate>
      <category>VB.NET</category>
    </item>
    <item>
      <title>Re: Read from and write to a text file in vb.net</title>
      <link>http://www.programmersheaven.com/mb/VBNET/267668/431157/re-read-from-and-write-to-a-text-file-in-vbnet/#431157</link>
      <description>Here's a simple explanation on how to read data from and write data to text file in asp.net &lt;a href="http://www.freedotnetapps.com/asp-net/how-to-read-data-from-and-write-data-to-text-file-in-asp-net/"&gt;http://www.freedotnetapps.com/asp-net/how-to-read-data-from-and-write-data-to-text-file-in-asp-net/&lt;/a&gt;&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/VBNET/267668/431157/re-read-from-and-write-to-a-text-file-in-vbnet/#431157</guid>
      <pubDate>Sat, 02 Feb 2013 03:54:57 -0700</pubDate>
      <category>VB.NET</category>
    </item>
  </channel>
</rss>