My.computer cannot access the file after file.create

I have code to delete a file, create another (so that I can rewrite it) and write on it.

My.Computer.FileSystem.DeleteFile("./pass") File.Create("./pass") My.Computer.FileSystem.WriteAllText("./pass", MaskedTextBox1.Text, True) 

When he begins to write the text, he says:

 An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll Additional information: The process cannot access the file '[path]\pass' because it is being used by another process. 

Is there a way to resolve this issue, or maybe just just overwrite the file without deleting it and creating it again?

+4
source share
1 answer

because it is used by another process

This is not entirely accurate; it is used by your process. File.Create () returns a FileStream object. You did not call the Close () method, so it is still in use. File.Create (). Close () will solve the problem.

But it makes no sense to use File.Create () here, FileSystem.WriteAllText () already creates the file. It makes no sense to delete the file, WriteAllText () overwrites the file. So just remove the first two statements to fix your problem.

+7
source

All Articles