为了避免这样的例外测试文件的可访问性
(1),因为它是由另一个进程
我用下面的方法来测试的文件之前任何可访问过程不能访问该文件进一步处理。
private bool CheckIfFileBeingUsed(string FilePath)
{
FileStream Fs = null;
try
{
Fs = File.Open(FilePath, FileMode.Open, FileAccess.Read, FileShare.None);
Fs.Close();
}
catch (Exception)
{
return true; //Error File is being used
}
return false; //File is not being used.
}
谁能告诉我有任何的Windows API或其他解决方案文件可访问的,而不是上述File.Open法这样的测试?
相关:http://stackoverflow.com/questions/1304/how-to-check-for-file-lock-in-c – Daniel
术语困惑:要检查文件是否已被锁定,但通常“无障碍“将指您是否拥有使用该文件的访问权限。 – Richard
亲爱的主席先生,会得到文件的属性导致访问/读取文件?如果文件没有准备好,那会导致异常? – Derek