Wrox Programmer Forums
VB.NET General VB.NET discussions for issues that don't fall into other VB.NET forums.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB.NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old December 1st, 2005, 05:46 PM
mh mh is offline
Authorized User
Join Date: Mar 2005
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
Default Editing a Text File


I'm trying to change the date in a text file to 14 days later once a button is clicked. The following is my code. It does not seem to do anything at all. Can someone please tell me what is going on? Thank you very much.

Imports System
Imports System.IO
Public Class WebForm1
    Inherits System.Web.UI.Page

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim Days As Double
            Dim IntervalType As DateInterval
            Dim SecondDate As Date
            IntervalType = DateInterval.Day
            Days = 14
            ' Create an instance of StreamReader to read from a file.
            Dim sr As StreamReader = New StreamReader("C:\Inetpub\wwwroot\CPS3\FIDELITY0921 2005.txt")
            Dim sw As StreamWriter = New StreamWriter("C:\Inetpub\wwwroot\CPS3\FIDELITY0921 2005.txt")
            Dim line, dt, newdate As String
            Dim i As Integer
            line = sr.ReadLine()
            line = sr.ReadLine()
            ' Add some text to the file.
            sr.BaseStream.Seek(65, 71)
            For i = 0 To 6
                dt = sr.Read.ToString
                If i = 0 Then
                    SecondDate = dt
                    SecondDate = SecondDate & dt
                End If
            newdate = DateAdd(IntervalType, Days, CDate(SecondDate))
            sw.BaseStream.Seek(65, 71)
            ' Let the user know what went wrong.
            Console.WriteLine("The file could not be read:")
        End Try
    End Sub
End Class

Old December 8th, 2005, 02:37 AM
Friend of Wrox
Join Date: Feb 2004
Posts: 177
Thanks: 0
Thanked 0 Times in 0 Posts

When you open a file with StreamWriter mode its basically for file writing so if there is a file with that name it deletes the contents in it.

So what you need to do is use StreamWriter with a different file name say "FIDELITY09212005_bak.txt" and then at the end rename the file to "FIDELITY09212005.txt".

It is not how much we do,
but how much love we put in the doing.

-Mother Theresa

Similar Threads
Thread Thread Starter Forum Replies Last Post
Selecting Text When Editing Datagrid elygp ASP.NET 1.0 and 1.1 Professional 1 May 29th, 2007 04:58 PM
Editing text in a richtextbox ja8261 VB.NET 2002/2003 Basics 5 March 9th, 2005 04:10 PM
Terrible Editing, Broken Examples, Rotten text treycarroll Wrox Book Feedback 26 October 31st, 2004 07:44 PM
.NET Text / HTML editing problem clickco General .NET 3 May 12th, 2004 02:47 PM
text boxes / data grids/ editing and paging Barb Lehman Classic ASP Basics 0 December 4th, 2003 12:55 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.