c# - 锁定文件 block

标签 c# .net

我有一个 192k 大小的文件,我想锁定文件的中间部分。

Ex) 我想用 C# 锁定文件的 64k-128k。知道如何锁定文件的那部分吗?

最佳答案

您需要使用 LockFileEx

Locks the specified file for exclusive access by the calling process. This function can operate either synchronously or asynchronously and can request either an exclusive or a shared lock.

您正在寻找共享锁。

[DllImport("kernel32.dll")]
static extern bool LockFileEx(IntPtr hFile, uint dwFlags, uint dwReserved,
   uint nNumberOfBytesToLockLow, uint nNumberOfBytesToLockHigh,
   [In] ref System.Threading.NativeOverlapped lpOverlapped);

关于c# - 锁定文件 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12478271/

相关文章:

c# - 在 .NET CF 中预处理和搜索文本文件的最佳方式

c# - 如何最好地等待文件锁释放

c# - 如何从 Url 下载图像并将其保存到本地 SQLite 数据库

c# - 在 C# 中相当于 Haskell 的 Data.List.Span

c# - 如何在 C# 中将字典初始化为 (key, value, value) 对

c# - .NET 选择项目对话框

c# - Entity Framework ,导航属性和存储库模式

c# - 将特殊字符从 MS-Word 或 Open Office writer 转换为记事本

C#/WinForms : ShowDialog and subsequent Show on Form

C# 如何从 WinForms 中的 ComboBox 获取枚举值?