VB.NET中,使用my.computer.file.create创建文件后,为何无法通过my.computer访问该文件?
- 内容介绍
- 文章标签
- 相关推荐
本文共计313个文字,预计阅读时间需要2分钟。
我有一些代码,需要删除一个文件,然后创建并覆盖另一个文件。代码如下:
My.Computer.FileSystem.DeleteFile(%2Fpass)File.Create(%2Fpass)My.Computer.FileSystem.WriteAllText(%2Fpass, MaskedTextBox1.Text, True)当它写入文字时
我有一些代码删除一个文件,另一个(所以我可以覆盖它)并写上它.My.Computer.FileSystem.DeleteFile("./pass") File.Create("./pass") My.Computer.FileSystem.WriteAllText("./pass", MaskedTextBox1.Text, True)
当它写出文字时,它说:
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.
有没有办法解决这个问题,或者可能只是覆盖文件而不删除并重新创建文件?
because it is being used by another process
这并不完全准确,它正由您的流程使用. File.Create()返回一个FileStream对象.你没有调用它的Close()方法,因此它仍在使用中. File.Create().Close()可以解决问题.
但是在这里使用File.Create()没有意义,FileSystem.WriteAllText()已经创建了该文件.删除文件也没有意义,WriteAllText()会覆盖文件.所以只需删除前两个语句来解决您的问题.
本文共计313个文字,预计阅读时间需要2分钟。
我有一些代码,需要删除一个文件,然后创建并覆盖另一个文件。代码如下:
My.Computer.FileSystem.DeleteFile(%2Fpass)File.Create(%2Fpass)My.Computer.FileSystem.WriteAllText(%2Fpass, MaskedTextBox1.Text, True)当它写入文字时
我有一些代码删除一个文件,另一个(所以我可以覆盖它)并写上它.My.Computer.FileSystem.DeleteFile("./pass") File.Create("./pass") My.Computer.FileSystem.WriteAllText("./pass", MaskedTextBox1.Text, True)
当它写出文字时,它说:
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.
有没有办法解决这个问题,或者可能只是覆盖文件而不删除并重新创建文件?
because it is being used by another process
这并不完全准确,它正由您的流程使用. File.Create()返回一个FileStream对象.你没有调用它的Close()方法,因此它仍在使用中. File.Create().Close()可以解决问题.
但是在这里使用File.Create()没有意义,FileSystem.WriteAllText()已经创建了该文件.删除文件也没有意义,WriteAllText()会覆盖文件.所以只需删除前两个语句来解决您的问题.

