gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 23:35:27 24 4
gpt4 key购买 nike

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

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

System.IO.FileStream.Unlock

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

这两种方法中的任何一种都行得通吗?如果是这样,能否请您提供一个样本,因为我还没有让其中任何一个起作用。或者,还有其他更好的方法吗?

最佳答案

您应该使用延迟文件操作,请参阅 How To Move Files That Are Currently in Use (“移动”在此上下文中包括删除)。

UnlockFile 和 friends 用于文件区域锁定操作,而不用于文件句柄锁定,参见 Locking and Unlocking Byte Ranges in Files .希望没有 API 可以解锁锁定的文件句柄,这会使锁定变得非常有用......

关于c# - 如何在 C# 中强制解锁文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2225733/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com