process cannot access the file

jkatshor

Member
Joined
Mar 20, 2009
Messages
11
Programming Experience
Beginner
Hello all,
I have a problem and have tried several attempts to remedy, but have not figured out a "robust" fix.

my problem is I randomly get the "process cannot access the file ??? because it is being used by another process"

What I am doing is reading a text file about every 1/2 second. later in my app i try to write to the same text file.

Question: is there a way that i can read the text file without causing this error?
an example would be greatly appreciated.

Thank you
 
hi hope this help, if you allready haven't figured it out.

Dim strString As System.IO.StreamWriter = New System.IO.StreamWriter("SomeText.txt")
strString.Write("Some String") ' Writes string to "SomeText.txt"
strString.Close() 'Closes the Stream to the Current file

also here a bit more information on this subject:
StreamWriter Class (System.IO)
 
resolved

All,
Thanks for the help.
Below is what I came up with, the file will continually loop until the read or write is completed.




HTML:
        Do
            Try
                If My.Computer.FileSystem.FileExists("C:\UCAL\currentpalletpositions") Then
                    Using reader As IO.StreamReader = New IO.StreamReader("C:\UCAL\currentpalletpositions")
                        PalletCntrTotalTextBox.Text = reader.ReadToEnd
                        PalletCntrTotalTextBox.Text = Split(PalletCntrTotalTextBox.Text, "lastpallet=" & Chr(34))(1)
                        Try
                            PalletCntrTotalTextBox.Text = Split(PalletCntrTotalTextBox.Text, vbCrLf)(0)
                        Catch ex As Exception
                            PalletCntrTotalTextBox.Text = 1
                        End Try
                        PalletCntrTotalTextBox.Text = Split(PalletCntrTotalTextBox.Text, vbCrLf)(0)
                    End Using
                    'PalletCntrTotalTextBox.Text = My.Computer.FileSystem.ReadAllText("C:\UCAL\currentpalletpositions")
                    'PalletCntrTotalTextBox.Text = Split(PalletCntrTotalTextBox.Text, "lastpallet=" & Chr(34))(1)
                    'PalletCntrTotalTextBox.Text = Split(PalletCntrTotalTextBox.Text, vbCrLf)(0)
                End If
                Exit Do
            Catch ex As Exception
                'MsgBox("Can't read" & vbCrLf & ex.Message)
            End Try
            Application.DoEvents()
        Loop
 
Back
Top