如何在c#中强制解锁文件?

alc*_*cal 5 c# io file-io

我需要删除一个文件.有时,文件可能会被锁定,在这种情况下,我想解锁并删除它.

到目前为止,我在研究中遇到了两种可能性.

System.IO.FileStream.Unlock

//unlock file
[DllImport("kernel32.dll", SetLastError = true)]
internal static extern bool UnlockFile(IntPtr handle, int offsetLow, int offsetHi);`  
Run Code Online (Sandbox Code Playgroud)

这两种方法中的任何一种都有效吗?如果是这样,请你提供样品,因为我没有得到这些中的任何一个.或者,还有另一种更好的方法吗?

alc*_*cal 1

最终使用 PSTools 实用程序,似乎工作正常。