加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

vb.net – my.computer在file.create之后无法访问文件

发布时间:2020-12-17 07:16:04 所属栏目:百科 来源:网络整理
导读:我有一些代码删除一个文件,另一个(所以我可以覆盖它)并写上它. My.Computer.FileSystem.DeleteFile("./pass") File.Create("./pass") My.Computer.FileSystem.WriteAllText("./pass",MaskedTextBox1.Text,True) 当它写出文字时,它说: An unhandled exceptio
我有一些代码删除一个文件,另一个(所以我可以覆盖它)并写上它.

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()会覆盖文件.所以只需删除前两个语句来解决您的问题.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读