我正在尝试使用该函数获取FILE_ID_BOTH_DIR_INFO 。GetFileInformationByHandle
调用后我的所有值都设置为零。
我收到 win32 错误消息:
ERROR_BAD_LENGTH 程序发出命令但命令长度不正确。
IntPtr handle = CreateFile(dir, EFileAccess.GenericRead, EFileShare.Read, IntPtr.Zero, ECreationDisposition.OpenExisting, EFileAttributes.BackupSemantics | EFileAttributes.Normal, IntPtr.Zero);
FILE_ID_BOTH_DIR_INFO fileStruct = new FILE_ID_BOTH_DIR_INFO();
GetFileInformationByHandleEx(handle, FILE_INFO_BY_HANDLE_CLASS.FileIdBothDirectoryInfo, out fileStruct, (uint)Marshal.SizeOf(fileStruct));
FILE_ID_BOTH_DIR_INFO 结构
typedef struct _FILE_ID_BOTH_DIR_INFO {
DWORD NextEntryOffset;
DWORD FileIndex;
LARGE_INTEGER CreationTime;
LARGE_INTEGER LastAccessTime;
LARGE_INTEGER LastWriteTime;
LARGE_INTEGER ChangeTime;
LARGE_INTEGER EndOfFile;
LARGE_INTEGER AllocationSize;
DWORD FileAttributes;
DWORD FileNameLength;
DWORD EaSize;
CCHAR ShortNameLength;
WCHAR ShortName[12];
LARGE_INTEGER FileId;
WCHAR FileName[1];
} FILE_ID_BOTH_DIR_INFO, *PFILE_ID_BOTH_DIR_INFO
在 C# 中
public struct FILE_ID_BOTH_DIR_INFO
{
uint NextEntryOffset;
uint FileIndex;
LARGE_INTEGER CreationTime;
LARGE_INTEGER LastAccessTime;
LARGE_INTEGER LastWriteTime;
LARGE_INTEGER ChangeTime;
LARGE_INTEGER EndOfFile;
LARGE_INTEGER AllocationSize;
uint FileAttributes;
uint FileNameLength;
uint EaSize;
char ShortNameLength;
[MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 12)]
string ShortName;
LARGE_INTEGER FileId;
[MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 1)]
string FileName;
}
[StructLayout(LayoutKind.Explicit, Size = 8)]
struct LARGE_INTEGER
{
[FieldOffset(0)]
public Int64 QuadPart;
[FieldOffset(0)]
public UInt32 LowPart;
[FieldOffset(4)]
public Int32 HighPart;
}