0

This is the code. It checks if the file in path exists, if not, it creates the file. I'm getting this error message all the time and I don't know why. Maybe I should close the System.IO.Directory.Exists? If yes, how do I do that? Just so you know, I'm creating a text file.

The code

If Not (System.IO.Directory.Exists(path)) Then
        Dim fs3 As FileStream = File.Create(path)
    End If

This is the error message I get:

Process can't use the file (path) because some other process is using this file at the moment.

user3364046
  • 55
  • 1
  • 7
  • I suspect the reason behind the problem is the same as in [IO Exception was unhandled error](http://stackoverflow.com/questions/22577442/io-exception-was-unhandled-error/22577562#comment34384863_22577562). Otherwise, is there are reason that you are using `Directory.Exists` instead of `File.Exists`? – Andrew Morton Apr 08 '14 at 12:10
  • It works now. I would never have thought of that, so thank you random stranger! Thumbs up for you. – user3364046 Apr 08 '14 at 12:13

1 Answers1

-1

The file is used by other processes hence it can't be overwritten. I suggest you delete the file first.

Dim path As String = "put your path"

For Each path In System.IO.Directory.GetFiles("C:\WINDOWS\TEMP")
System.IO.File.Delete(path)
Next path

Dim fs3 As FileStream = File.Create(path)

Be certain that you have full rights [under properties] to the folder.

Bonga Mbombi
  • 175
  • 1
  • 11