The process cannot access the file XXX because it is being used by another process

  • Post author:
  • Post category:其他


如下代码

每次新建文件的时候总是报错

The process cannot access the file ‘D:\errorlog\2011年7月23日.txt’ because it is being used by another process.

Public Sub writelog(ByVal message As String, ByVal path As String)

Dim writer As StreamWriter

If File.Exists(path) Then

Else


File.Create(path)

End If

Dim logfile As FileInfo = New FileInfo(path)

writer = File.AppendText(path)

writer.WriteLine(message)

writer.Flush()

writer.Close()

End Sub

经过尝试,原因是黄色代码的位置出现了问题。

file.creat()命令执行后,并不会释放资源,造成后边streamwriter访问被独占。

解决办法也简单改成


File.Create(path).close() 即可。



版权声明:本文为pumaadamsjack原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。