0

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.

Hannele
  • 9,301
  • 6
  • 48
  • 68
aarviii
  • 13
  • 1
  • 1
  • 6
  • Based on the same problem I have seen elsewhere several times, the chances are that you have already opened the file somewhere else in your program and forgotten to close it, so that is the first thing to check. – Andrew Morton Apr 02 '13 at 18:40
  • i tried to search, i found some answers but that is not what i need, i just want a code how to stop it. – aarviii Apr 02 '13 at 18:46
  • If you are convinced that it is not your program which has another handle to the file, you need to investigate which other program does (it *could* be your anti-virus software). You can use Process Explorer from http://www.sysinternals.com/ or it would probably be simpler to use Unlocker ( http://www.emptyloop.com/unlocker/ ). – Andrew Morton Apr 02 '13 at 18:56
  • its just only my program that uses this file. its because whenever the form is called it calls the data which are already inputted from that txt file. – aarviii Apr 02 '13 at 19:11
  • If you're reading data from the file initially, you should make sure that you also closed your Stream *Reader*. – Hannele Apr 02 '13 at 19:21
  • wow. that code works amazing. big thanks! ms@hannele :D – aarviii Apr 02 '13 at 19:40
  • All I did was make it a little easier to read, and added the `End Sub` - just make sure you're closing your subroutines! – Hannele Apr 03 '13 at 12:52
  • Note, that `write.close()` only closes your StreamWriter - you can do other things in that same subroutine even after that's happened. To close `Sub WRITEDATA()`, you need to put `End Sub`. – Hannele Apr 03 '13 at 12:55

1 Answers1

0

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

Community
  • 1
  • 1
Jim Wooley
  • 10,169
  • 1
  • 25
  • 43