您的 P/Invoke 签名有点不对劲:
这是 Win32 的定义:
DWORD WINAPI SetFilePointer(
_In_ HANDLE hFile,
_In_ LONG lDistanceToMove,
_Inout_opt_ PLONG lpDistanceToMoveHigh,
_In_ DWORD dwMoveMethod
);
这是指定了枚举的 P/Invoke:
[DllImport("kernel32.dll", EntryPoint="SetFilePointer")]
static extern uint SetFilePointer(
[In] Microsoft.Win32.SafeHandles.SafeFileHandle hFile,
[In] int lDistanceToMove,
[In, Out] ref int lpDistanceToMoveHigh,
[In] EMoveMethod dwMoveMethod) ;
编辑:哦,还有一些测试代码:
var text = "Here is some text to put in the test file";
File.WriteAllText(@"c:\temp\test.txt", text);
var file = File.Open(@"c:\temp\test.txt", FileMode.OpenOrCreate);
int moveDistance = 10;
int moveDistanceHighBits = 0;
uint retvalUint = SetFilePointer(file.SafeFileHandle, moveDistance, ref moveDistanceHighBits, EMoveMethod.Begin);
Debug.Assert(Encoding.ASCII.GetBytes(text)[moveDistance] == file.ReadByte());
另请注意文档:
l 移动距离 [in]
有符号值的低位 32 位,指定移动文件指针的字节数。
如果 lpDistanceToMoveHigh 不为 NULL,则 lpDistanceToMoveHigh 和 lDistanceToMove 形成单个 64 位有符号值,指定要移动的距离。
如果 lpDistanceToMoveHigh 为 NULL,则 lDistanceToMove 是一个 32 位有符号值。 lDistanceToMove 的正值会将文件指针在文件中向前移动,负值会将文件指针向后移动。
lpDistanceToMoveHigh [输入、输出、可选]
指向要移动的有符号 64 位距离的高位 32 位的指针。
如果不需要高位 32 位,则必须将该指针设置为 NULL。
当不为 NULL 时,该参数还接收文件指针新值的高位 DWORD。有关详细信息,请参阅本主题中的备注部分。