Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Working with StreamReader/Writer

Working with StreamReader/Writer

Scheduled Pinned Locked Moved Visual Basic
questionhelptutorial
15 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Taen_Karth

    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.

    S Offline
    S Offline
    Stephen McGuire
    wrote on last edited by
    #2

    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

    T 1 Reply Last reply
    0
    • S Stephen McGuire

      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

      T Offline
      T Offline
      Taen_Karth
      wrote on last edited by
      #3

      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

      S 1 Reply Last reply
      0
      • T 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

        S Offline
        S Offline
        Stephen McGuire
        wrote on last edited by
        #4

        You don't have the file open in another app do you? Notepad or Word etc? Steve

        T 1 Reply Last reply
        0
        • S Stephen McGuire

          You don't have the file open in another app do you? Notepad or Word etc? Steve

          T Offline
          T Offline
          Taen_Karth
          wrote on last edited by
          #5

          Nope. :confused:

          Thanks, Taen Karth

          S 1 Reply Last reply
          0
          • T Taen_Karth

            Nope. :confused:

            Thanks, Taen Karth

            S Offline
            S Offline
            Stephen McGuire
            wrote on last edited by
            #6

            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

            S T 2 Replies Last reply
            0
            • S Stephen McGuire

              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

              S Offline
              S Offline
              Stephen McGuire
              wrote on last edited by
              #7

              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

              1 Reply Last reply
              0
              • S Stephen McGuire

                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

                T Offline
                T Offline
                Taen_Karth
                wrote on last edited by
                #8

                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

                S 1 Reply Last reply
                0
                • T 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

                  S Offline
                  S Offline
                  Stephen McGuire
                  wrote on last edited by
                  #9

                  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

                  T 1 Reply Last reply
                  0
                  • S Stephen McGuire

                    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

                    T Offline
                    T Offline
                    Taen_Karth
                    wrote on last edited by
                    #10

                    Still the same "File is in use" error as before...

                    Thanks, Taen Karth

                    T 1 Reply Last reply
                    0
                    • T Taen_Karth

                      Still the same "File is in use" error as before...

                      Thanks, Taen Karth

                      T Offline
                      T Offline
                      Taen_Karth
                      wrote on last edited by
                      #11

                      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

                      S 1 Reply Last reply
                      0
                      • T 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

                        S Offline
                        S Offline
                        Stephen McGuire
                        wrote on last edited by
                        #12

                        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

                        T 1 Reply Last reply
                        0
                        • S Stephen McGuire

                          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

                          T Offline
                          T Offline
                          Taen_Karth
                          wrote on last edited by
                          #13

                          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

                          S 1 Reply Last reply
                          0
                          • T 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

                            S Offline
                            S Offline
                            Stephen McGuire
                            wrote on last edited by
                            #14

                            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

                            T 1 Reply Last reply
                            0
                            • S Stephen McGuire

                              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

                              T Offline
                              T Offline
                              Taen_Karth
                              wrote on last edited by
                              #15

                              Thanks again that did it!:-D

                              Thanks, Taen Karth

                              1 Reply Last reply
                              0
                              Reply
                              • Reply as topic
                              Log in to reply
                              • Oldest to Newest
                              • Newest to Oldest
                              • Most Votes


                              • Login

                              • Don't have an account? Register

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • World
                              • Users
                              • Groups