简体   繁体   中英

killing process used by vb.net

I need some help with my program. I want to rewrite the data on my .txt file but an error occurs:

The process cannot access the file 'C:\\Users\\AARVIII\\Documents\\Visual Studio 2010\\Projects\\PROJECT\\WindowsApplication3\\bin\\Debug\\ORDERS\\aa.txt' because it is being used by another process.

Here is the code:

Sub WRITEDATA()

    Dim write As New System.IO.StreamWriter("ORDERS\" & TBFNAME.Text + "" + TBLNAME.Text & ".txt", False)
    write.WriteLine(TBFNAME.Text)
    write.WriteLine(TBLNAME.Text)
    write.WriteLine(TBEADD.Text)
    write.WriteLine(TBEADD2.Text)
    write.WriteLine(TBADDRESS.Text)
    write.WriteLine(TBCONTACT.Text)
    write.close()

End Sub

I used a StreamReader to get the data which had already been put in that text file. Please help me figure out how to kill that process so that I can rewrite my data.

It is very possible that your app (on another thread?) is the culprit. First, to make sure you release the resource, make sure to wrap your code in a using block:

  Using Dim write As New System.IO.StreamWriter("ORDERS\" & TBFNAME.Text + "" + TBLNAME.Text & ".txt", False)
        write.WriteLine(TBFNAME.Text)
        write.WriteLine(TBLNAME.Text)
        write.WriteLine(TBEADD.Text)
        write.WriteLine(TBEADD2.Text)
        write.WriteLine(TBADDRESS.Text)
        write.WriteLine(TBCONTACT.Text)
  End Using

Additionally, you may want to see this thread: .NET Asynchronous stream read/write

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM