C# 错误提示:IOException: Sharing violation on path
路径上的共享冲突
代码里检测文件是否存在,如果不存在,则创建文件时:
if (!File.Exists(path))
{
File.Create(path);
}
提示出现 IOException: Sharing violation on path 的错误。
解决方案:
在创建文件后面加上 Dispose()
函数即可,更改后的代码如下所示:
if (!File.Exists(path))
{
File.Create(path).Dispose();
}