Working with StreamReader/Writer
-
I have a simple project that i am trying to figure out how to complete. I have a .txt file that I am reading from. I would like to be able to read it into a RTF control change the text around and save it back. I was trying to make a shortcut and delete the file then create it and write the information from the RTF box or a stored string variable but no luck...something is holding the file open and I cannot figure out what it is. Code is below.
Public Class Summary Dim path As String = Application.StartupPath & "\" Dim fileName As String = "ChangeLog.txt" Dim MyString As String Private Sub Summary_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim Sr As New IO.StreamReader(path & fileName, True) Sr.BaseStream.Seek(0, IO.SeekOrigin.Begin) Do While Sr.Peek > -1 MyString = Sr.ReadToEnd Loop With rtSummary .ReadOnly = True .Text = MyString End With Sr.Close() End Sub Private Sub ToolStripButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbtnSave.Click Dim Sw As New IO.StreamWriter(path & fileName, False) If IO.File.Exists(path & fileName) Then IO.File.Delete(fileName) IO.File.Create(fileName) Sw.WriteLine(vbCrLf & MyString & vbCrLf & rtSummary.Text) Sw.Flush() Sw.Close() Else IO.File.Create(path & fileName) Sw.WriteLine(vbCrLf & rtSummary.Text) Sw.Flush() Sw.Close() End If Me.Dispose() End Sub Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbtnEdit.Click rtSummary.ReadOnly = False End Sub Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbtnClear.Click rtSummary.Clear() End Sub Private Sub Summary_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing If rtSummary.Text IsNot "" Then End If End Sub End Class
I know that some of this has no relevance to the problem but i thought i would include it nonetheless. What is holding the fle open so i cannot delete it. Or...is there a better way to accomplish what I am trying to accomplish? Thanks in advance.Can you describe what is happening? Is your textbox getting the text? Your Sr.ReadToEnd should not be in a loop, since you are not reading each line...you are already reading the whole stream in one hit! You should also close the SteamReader as soon as you have finished with it. Before you do anything with the data retrieved... Steve -- modified at 14:54 Monday 14th August, 2006
-
Can you describe what is happening? Is your textbox getting the text? Your Sr.ReadToEnd should not be in a loop, since you are not reading each line...you are already reading the whole stream in one hit! You should also close the SteamReader as soon as you have finished with it. Before you do anything with the data retrieved... Steve -- modified at 14:54 Monday 14th August, 2006
Well that sure makes sense!...hehe My RTF box IS in fact gettnig all the text, What is happening is that when it gets to the line: IO.File.Delete(fileName) It states that it cannot perform the action because the file requested is still in use. Loop removed.
Thanks, Taen Karth
-
Well that sure makes sense!...hehe My RTF box IS in fact gettnig all the text, What is happening is that when it gets to the line: IO.File.Delete(fileName) It states that it cannot perform the action because the file requested is still in use. Loop removed.
Thanks, Taen Karth
You don't have the file open in another app do you? Notepad or Word etc? Steve
-
You don't have the file open in another app do you? Notepad or Word etc? Steve
Nope. :confused:
Thanks, Taen Karth
-
Nope. :confused:
Thanks, Taen Karth
You only have 'filname' for the file to delete. Shouldn't this be 'path & filename'? If you are always going to delete the file anyway, you don't have to check whether it exists first. Just code for deleting the file. An exception will not be generated if the file doesn't exist... Then try using the CreateText method of the IO.File class. What is that Me.Dispose() supposed to be doing? Steve -- modified at 15:26 Monday 14th August, 2006
-
You only have 'filname' for the file to delete. Shouldn't this be 'path & filename'? If you are always going to delete the file anyway, you don't have to check whether it exists first. Just code for deleting the file. An exception will not be generated if the file doesn't exist... Then try using the CreateText method of the IO.File class. What is that Me.Dispose() supposed to be doing? Steve -- modified at 15:26 Monday 14th August, 2006
Put your StreamWriter contstructor AFTER your File.Create line. I think this is holding the file open. You should only put this after you have created the file! Steve -- modified at 15:37 Monday 14th August, 2006
-
You only have 'filname' for the file to delete. Shouldn't this be 'path & filename'? If you are always going to delete the file anyway, you don't have to check whether it exists first. Just code for deleting the file. An exception will not be generated if the file doesn't exist... Then try using the CreateText method of the IO.File class. What is that Me.Dispose() supposed to be doing? Steve -- modified at 15:26 Monday 14th August, 2006
Ok tried that and still getting this error message:
The process cannot access the file 'C:\Documents and Settings\troy\Desktop\Projects\ChangeLogger\ChangeLogger\bin\Debug\ChangeLog.txt' because it is being used by another process.
This is what the "Save" event looks like now:
Private Sub ToolStripButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbtnSave.Click Dim Sw As New IO.StreamWriter(path & fileName, False) IO.File.Delete(path & fileName) IO.File.CreateText(path & fileName) Sw.WriteLine(vbCrLf & MyString & vbCrLf & rtSummary.Text) Sw.Flush() Sw.Close() Me.Dispose() End Sub
Any ideas? btw thanks for the suggestions so far!Thanks, Taen Karth
-
Ok tried that and still getting this error message:
The process cannot access the file 'C:\Documents and Settings\troy\Desktop\Projects\ChangeLogger\ChangeLogger\bin\Debug\ChangeLog.txt' because it is being used by another process.
This is what the "Save" event looks like now:
Private Sub ToolStripButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbtnSave.Click Dim Sw As New IO.StreamWriter(path & fileName, False) IO.File.Delete(path & fileName) IO.File.CreateText(path & fileName) Sw.WriteLine(vbCrLf & MyString & vbCrLf & rtSummary.Text) Sw.Flush() Sw.Close() Me.Dispose() End Sub
Any ideas? btw thanks for the suggestions so far!Thanks, Taen Karth
The StreamWriter constructor must be moved to here:
Private Sub ToolStripButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbtnSave.Click IO.File.Delete(path & fileName) IO.File.CreateText(path & fileName) Dim Sw As New IO.StreamWriter(path & fileName, False) Sw.WriteLine(vbCrLf & MyString & vbCrLf & rtSummary.Text) Sw.Flush() Sw.Close() End Sub
I have also removed the Me.Dispose() line. If you want to do that, do it in the form's closed event handler. Steve -
The StreamWriter constructor must be moved to here:
Private Sub ToolStripButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbtnSave.Click IO.File.Delete(path & fileName) IO.File.CreateText(path & fileName) Dim Sw As New IO.StreamWriter(path & fileName, False) Sw.WriteLine(vbCrLf & MyString & vbCrLf & rtSummary.Text) Sw.Flush() Sw.Close() End Sub
I have also removed the Me.Dispose() line. If you want to do that, do it in the form's closed event handler. SteveStill the same "File is in use" error as before...
Thanks, Taen Karth
-
Still the same "File is in use" error as before...
Thanks, Taen Karth
Jeez...now I get that error on this line:
Dim Sw As New IO.StreamWriter(path & fileName, False)
and I do not get any errors on the two lines right before it that create adn delete the file.Thanks, Taen Karth
-
Jeez...now I get that error on this line:
Dim Sw As New IO.StreamWriter(path & fileName, False)
and I do not get any errors on the two lines right before it that create adn delete the file.Thanks, Taen Karth
Sorry. I forgot to move the other line. Should be like this:
Private Sub ToolStripButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbtnSave.Click IO.File.Delete(path & fileName) Dim Sw As IO.StreamWriter = IO.File.CreateText(path & fileName) Sw.WriteLine(vbCrLf & MyString & vbCrLf & rtSummary.Text) Sw.Flush() Sw.Close() End Sub
Steve -
Sorry. I forgot to move the other line. Should be like this:
Private Sub ToolStripButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbtnSave.Click IO.File.Delete(path & fileName) Dim Sw As IO.StreamWriter = IO.File.CreateText(path & fileName) Sw.WriteLine(vbCrLf & MyString & vbCrLf & rtSummary.Text) Sw.Flush() Sw.Close() End Sub
SteveThat did it...and now i understand why...lol. Thanks for sticking through me with this one. I really appreciate it. Now i just have to remeber how to capture the button result from a messagebox button when clicked...lol I do believe I am a bit rusty now. Once again thanks fr the help!:-D
Thanks, Taen Karth
-
That did it...and now i understand why...lol. Thanks for sticking through me with this one. I really appreciate it. Now i just have to remeber how to capture the button result from a messagebox button when clicked...lol I do believe I am a bit rusty now. Once again thanks fr the help!:-D
Thanks, Taen Karth
Great! Glad I could help. To get the message box button clicked, check the DialogResult. If MessageBox.Show() = DialogResult.OK Then End If DialogResult options will vary depending on the button option you set for the dialog. Steve
-
Great! Glad I could help. To get the message box button clicked, check the DialogResult. If MessageBox.Show() = DialogResult.OK Then End If DialogResult options will vary depending on the button option you set for the dialog. Steve
Thanks again that did it!:-D
Thanks, Taen Karth