安装:
Install-Package LockFileExtensions
示例代码:
var FilePath = @"E:\Temp\test2.docx";
var file = new FileInfo(FilePath);
if (file.Exists && file.IsLocked())
{Console.WriteLine("文件被占用");var items = file.GetLockProcesses();foreach (System.Diagnostics.Process? process in items){Console.WriteLine(process.Id);if (process.MainModule != null){Console.WriteLine(process.MainModule.FileName);}else{Console.WriteLine(process.ProcessName);}}
}
else
{Console.WriteLine("文件没有被占用");
}