Results 1 to 4 of 4

Thread: [RESOLVED] Reading and displaying text contents of an open txt file using rtb

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2009
    Location
    Nairobi
    Posts
    75

    Resolved [RESOLVED] Reading and displaying text contents of an open txt file using rtb

    I have a code that records all the user interaction with an application on a text file.

    Code:
        Public Shared fs As New FileStream(Application.LocalUserAppDataPath & "\mcb.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite)
        Public Shared m_streamWriter As New StreamWriter(fs)
    
        Private Sub frmMain_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            ' Write to the file using StreamWriter class
            m_streamWriter.BaseStream.Seek(0, SeekOrigin.End)
            m_streamWriter.Write(" File Write Operation Starts : ")
            m_streamWriter.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(), DateTime.Now.ToLongDateString())
            m_streamWriter.WriteLine(" ===================================== " + ControlChars.Lf)
            m_streamWriter.Flush()
        End Sub
    Now I want to display the contents of the text file on a richtextbox, but I'm getting an error. This is my code for displaying

    Code:
    Me.rtbTrack.LoadFile(Application.LocalUserAppDataPath & "\mcb.txt", RichTextBoxStreamType.PlainText)
    The error I'm getting is "The process cannot access the file 'C:\Users\Tum\AppData\Local\Powerware_System\Powerware System\1.0.0.0\mcb.txt' because it is being used by another process."

    Any suggesion of getting my way out of it? Is it possible to read an open file, open in the same application? Please lead me...

    Thank you..

  2. #2
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    Re: Reading and displaying text contents of an open txt file using rtb

    I beleive you need to close the streamwriter? and or the file
    alternatively you can use
    using and end using in your code to close it out
    Last edited by billboy; Jan 28th, 2013 at 01:27 AM.

  3. #3
    Frenzied Member IanRyder's Avatar
    Join Date
    Jan 2013
    Location
    Healing, UK
    Posts
    1,232

    Re: Reading and displaying text contents of an open txt file using rtb

    Hi,

    Typically, I would agree that you need to close a file before another process can use it. However, and after a quick play, I am surprised to find that using the FileSystem.CopyFile method will happily create a copy of a file even though you currently have that file open for writing in your project. See here:-

    Code:
    My.Computer.FileSystem.CopyFile("d:\temp\MyFile.txt", "d:\temp\Temp.txt", True)
    You have to ensure to use the Flush method of the StreamWriter to ensure data is flushed to the file but after this copy is called you just simply read the temporary file.

    Hope that helps.

    Cheers,

    Ian

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Aug 2009
    Location
    Nairobi
    Posts
    75

    Re: Reading and displaying text contents of an open txt file using rtb

    Thanks men, I found copying the file each time I want to read is better.. Solved

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width