如何使用ReadFile()Windows API(C#)读取特定偏移位置的文件?
以下C#摘要执行二进制文件中页面的顺序读取。
由于某些精致的原因 - 使用readfile()Windows System API是必须的。
for (iReadCounter = 0; iReadCounter < iReadCountLimit; iReadCounter++)
{
readsize = DefineConstants.READ_BUF_SIZE;
bool bResult = ReadFile(fhnd, readbuffer, (uint)readsize, out readresult, ref _overlapped);
.
.
}
是否有一种方法可以将 readfile()指向以特定的文件偏移/选择位置阅读?
谢谢。
/h
Following C# snippet executes sequential reads of pages in a binary files.
For certain delicate reasons - using ReadFile() Windows System API is a must.
for (iReadCounter = 0; iReadCounter < iReadCountLimit; iReadCounter++)
{
readsize = DefineConstants.READ_BUF_SIZE;
bool bResult = ReadFile(fhnd, readbuffer, (uint)readsize, out readresult, ref _overlapped);
.
.
}
Is there a way by which ReadFile() can be directed to read at a specific file offset/position of choice?
Thanks.
/H
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您将
lpoverlapped
readfile()
的参数设置为_Overlapped
变量的引用。因此,您需要使用_OVERLAPED.OFFSET
和_Overlapped.offsethigh
字段来指定所需的文件偏移以从中读取。per 代码> 文档:
“同步和文件位置”部分说:
您是否一直在设置
lpoverlapped
参数为null
而是您必须使用 a>或 a>指定所需偏移:You are setting the
lpOverlapped
parameter ofReadFile()
to a reference to an_overlapped
variable. So you need to use the_overlapped.Offset
and_overlapped.OffsetHigh
fields to specify the desired file offset to read from.Per the
ReadFile()
documentation:And the "Synchronization and File Position" section says:
Had you been setting the
lpOverlapped
parameter tonull
instead, then you would have to useSetFilePointer()
orSetFilePointerEx()
to specify the desired offset:如果该文件实际上支持在特定位置(不是给定)的读数,则您应该能够使用重叠参数的偏移和偏高成员来指定读取位置。
即使未打开对重叠的I/O打开文件,这也应该有效。
If the file actually supports reading at a specific position (not a given), you should be able to use the Offset and OffsetHigh members of the OVERLAPPED parameter to specify the read position.
This should work even if the file was not opened for overlapped I/O.
我相信您应该能够使用
setFilePointer
或setfilepointerex
进行此操作。它允许您设置文件句柄指向的位置。https://learn.microsoft.com/en-us/windows/wind32/api/fileapi/nf-fileapi-setfilepointer
I believe you should be able to do this with
SetFilePointer
orSetFilePointerEx
. It allows you to set where the file handle is pointing at.https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-setfilepointer