{"id":116,"date":"2006-12-03T03:54:59","date_gmt":"2006-12-03T08:54:59","guid":{"rendered":"\/cgi\/wp\/?p=116"},"modified":"2006-12-03T04:16:31","modified_gmt":"2006-12-03T09:16:31","slug":"aspnet-file-processing","status":"publish","type":"post","link":"http:\/\/williamsportwebdeveloper.com\/cgi\/wp\/?p=116","title":{"rendered":"ASP.NET File Processing"},"content":{"rendered":"<p>I recently worked on a project that required updating a specific line of text in a file. This proved to be surprisingly difficult because I&#8217;ve never done that in ASP.NET before and I could not find any sample code on the Internet. You cannot write to a file after opening it to read through the lines of text. I had to write to a temporary file, delete the original file, and then copy the temporary file with the original file name. A line counter is used to update the target line within the file. I created a generic version of this code for my notes:<\/p>\n<pre><font color=\"blue\"><b>&lt;%@ Import Namespace=&quot;System&quot; %&gt;<\/b><\/font>\r\n<font color=\"blue\"><b>&lt;%@ Import Namespace=&quot;System.IO&quot; %&gt;<\/b><\/font>\r\n<font color=\"blue\">&lt;<\/font><font color=\"maroon\">html<\/font><font color=\"blue\">&gt;<\/font>\r\n<font color=\"blue\">&lt;<\/font><font color=\"maroon\">title<\/font><font color=\"blue\">&gt;<\/font>Update Text File<font color=\"blue\">&lt;\/<\/font><font color=\"maroon\">title<\/font><font color=\"blue\">&gt;<\/font>\r\n<font color=\"blue\">&lt;<\/font><font color=\"maroon\">script<\/font> language=&quot;vb&quot; runat=&quot;server&quot;<font color=\"blue\">&gt;<\/font>\r\n<font color=\"green\">' requires ASPNET account to have write permissions on target directory<\/font>\r\n<font color=\"blue\">Private<\/font> <font color=\"blue\">Sub<\/font> btnUpdate_Click(o <font color=\"blue\">As<\/font> Object, e <font color=\"blue\">As<\/font> EventArgs)\r\n    <font color=\"blue\">Try<\/font>\r\n        <font color=\"blue\">Dim<\/font> w <font color=\"blue\">As<\/font> StreamWriter\r\n        w = File.CreateText(&quot;C:\/Inetpub\/wwwroot\/experiments\/temp.txt&quot;)\r\n        <font color=\"blue\">Dim<\/font> r <font color=\"blue\">As<\/font> StreamReader = File.OpenText(&quot;C:\/Inetpub\/wwwroot\/experiments\/test.txt&quot;)\r\n        <font color=\"blue\">Dim<\/font> intLineCount <font color=\"blue\">As<\/font> <font color=\"blue\">Integer<\/font>\r\n        <font color=\"blue\">Dim<\/font> strTextLine <font color=\"blue\">As<\/font> <font color=\"blue\">String<\/font>\r\n        <font color=\"blue\">Dim<\/font> strTodaysDate <font color=\"blue\">As<\/font> <font color=\"blue\">String<\/font>\r\n        \r\n        intLineCount = 0\r\n        <font color=\"blue\">While<\/font> r.Peek() &lt;&gt; -1\r\n            strTextLine = r.ReadLine()\r\n            intLineCount = intLineCount + 1\r\n            <font color=\"green\">' update the 5th line<\/font>\r\n            <font color=\"blue\">If<\/font> intLineCount = 5 <font color=\"blue\">Then<\/font>\r\n                strTodaysDate = Now()\r\n                w.WriteLine(strTodaysDate)\r\n            <font color=\"blue\">Else<\/font>\r\n                w.WriteLine(strTextLine)\r\n            <font color=\"blue\">End If<\/font>\r\n        End <font color=\"blue\">While<\/font>\r\n        \r\n        w.Close()\r\n        r.Close()\r\n        \r\n        <font color=\"blue\">Dim<\/font> f <font color=\"blue\">As<\/font> File\r\n        <font color=\"green\">' delete original file<\/font>\r\n        f.Delete(&quot;C:\/Inetpub\/wwwroot\/experiments\/test.txt&quot;)\r\n        <font color=\"green\">' copy the temp file to restore deleted file<\/font>\r\n        f.Copy(&quot;C:\/Inetpub\/wwwroot\/experiments\/temp.txt&quot;, &quot;C:\/Inetpub\/wwwroot\/experiments\/test.txt&quot;)\r\n        \r\n        lblMessage.Text = &quot;File Updated!&quot;\r\n    <font color=\"blue\">Catch<\/font> ex <font color=\"blue\">As<\/font> Exception\r\n            lblMessage.Text = ex.Message &amp; &quot;&lt;br&gt;&quot; &amp; ex.Source &amp; &quot;&lt;br&gt;&quot; &amp; ex.StackTrace &amp; &quot;&lt;br&gt;&quot;\r\n    <font color=\"blue\">End Try<\/font>\r\n<font color=\"blue\">End Sub<\/font>\r\n<font color=\"blue\">&lt;\/<\/font><font color=\"maroon\">script<\/font><font color=\"blue\">&gt;<\/font>\r\n<font color=\"blue\">&lt;<\/font><font color=\"maroon\">body<\/font><font color=\"blue\">&gt;<\/font>\r\n<font color=\"blue\">&lt;<\/font><font color=\"maroon\">h1<\/font> style=&quot;font-family:Arial&quot;<font color=\"blue\">&gt;<\/font>Update Text File<font color=\"blue\">&lt;\/<\/font><font color=\"maroon\">h1<\/font><font color=\"blue\">&gt;<\/font>\r\n<font color=\"blue\">&lt;<\/font><font color=\"maroon\">hr<\/font> size=&quot;1&quot; noshade<font color=\"blue\">&gt;<\/font>\r\n<font color=\"blue\">&lt;<\/font><font color=\"maroon\">form<\/font> runat=&quot;server&quot; id=&quot;form1&quot; name=&quot;form1&quot;<font color=\"blue\">&gt;<\/font>\r\n<font color=\"blue\">&lt;<b>asp:button id=&quot;btnUpdate&quot; text=&quot;Update File&quot; runat=&quot;server&quot; onclick=&quot;btnUpdate_Click&quot;&gt;&lt;\/asp:button&gt;<\/b><\/font>\r\n<font color=\"blue\">&lt;<\/font><font color=\"maroon\">br<\/font><font color=\"blue\">&gt;&lt;<\/font><font color=\"maroon\">br<\/font><font color=\"blue\">&gt;<\/font>\r\n<font color=\"blue\">&lt;<\/font><font color=\"maroon\">font<\/font> face=&quot;Arial&quot;<font color=\"blue\">&gt;&lt;<b>asp:label id=&quot;lblMessage&quot; runat=&quot;server&quot; \/&gt;&lt;\/<font color=\"maroon\">font<\/font>&gt;<\/b><\/font>\r\n<font color=\"blue\">&lt;\/<\/font><font color=\"maroon\">form<\/font><font color=\"blue\">&gt;<\/font>\r\n<font color=\"blue\">&lt;\/<\/font><font color=\"maroon\">body<\/font><font color=\"blue\">&gt;<\/font>\r\n<font color=\"blue\">&lt;\/<\/font><font color=\"maroon\">html<\/font><font color=\"blue\">&gt;<\/font><\/pre>\n<p><strong>NOTE:<\/strong> Due to some problems with WordPress I had to replace backslashes with forward slashes in the file paths.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I recently worked on a project that required updating a specific line of text in a file. This proved to be surprisingly difficult because I&#8217;ve never done that in ASP.NET before and I could not find any sample code on &hellip; <a href=\"http:\/\/williamsportwebdeveloper.com\/cgi\/wp\/?p=116\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"http:\/\/williamsportwebdeveloper.com\/cgi\/wp\/index.php?rest_route=\/wp\/v2\/posts\/116"}],"collection":[{"href":"http:\/\/williamsportwebdeveloper.com\/cgi\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/williamsportwebdeveloper.com\/cgi\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/williamsportwebdeveloper.com\/cgi\/wp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/williamsportwebdeveloper.com\/cgi\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=116"}],"version-history":[{"count":0,"href":"http:\/\/williamsportwebdeveloper.com\/cgi\/wp\/index.php?rest_route=\/wp\/v2\/posts\/116\/revisions"}],"wp:attachment":[{"href":"http:\/\/williamsportwebdeveloper.com\/cgi\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=116"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/williamsportwebdeveloper.com\/cgi\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=116"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/williamsportwebdeveloper.com\/cgi\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=116"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}