gpt4 book ai didi

vb.net - 我的电脑在file.create后无法访问文件

转载 作者:行者123 更新时间:2023-12-04 23:58:21 27 4
gpt4 key购买 nike

我有一些代码可以删除一个文件,创建另一个文件(这样我可以覆盖它)并在上面写。

            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() 会覆盖文件。因此,只需删除前两个语句即可解决您的问题。

关于vb.net - 我的电脑在file.create后无法访问文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13891617/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com