我试图通过 UNC 路径从 Windows 共享文件夹中读取文件正文,并收到此异常:The process cannot access the file '\\<someIP>\logs\LogFiles\W3SVC1\u_ex141017.log' because it is being used by another process.
但是,该文件并未真正被任何进程锁定。我可以使用文本编辑器等从我的电脑上查看它。
我正在使用此代码来读取文件:
var logFile = File.ReadAllText(logPath);
and
var logFile = (string)null;
using (var fileStream = new FileStream(logPath, FileMode.Open, FileAccess.Read, FileShare.Delete))
{
using (var reader = new StreamReader(fileStream))
{
logFile = reader.ReadToEnd();
}
}
(均失败)
当文件没有被任何进程真正锁定时,您知道为什么会发生此异常吗?
尝试将 FileShare.Delete 更改为 FileShare.ReadWrite。这将允许其他应用程序同时读取和写入该文件。换句话说
var logFile = (string)null;
using (var fileStream = new FileStream(logPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
using (var reader = new StreamReader(fileStream))
{
logFile = reader.ReadToEnd();
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)