3
如何獲取c#中文件夾的唯一NTFS文件ID?如何獲取文件夾的NTFS文件ID
如何獲取c#中文件夾的唯一NTFS文件ID?如何獲取文件夾的NTFS文件ID
internal static Int64 GetDirectoryId(string dir)
{
var handle = CreateFile(dir, FileAccess.Read, FileShare.Read, IntPtr.Zero, FileMode.Open, 0x02000000 | 0x00000080, IntPtr.Zero);
var fileStruct = new FILE_ID_BOTH_DIR_INFO();
GetFileInformationByHandleEx(handle, FILE_INFO_BY_HANDLE_CLASS.FileIdBothDirectoryInfo, out fileStruct, (uint)Marshal.SizeOf(fileStruct));
CloseHandle(handle);
var win32Error = Marshal.GetLastWin32Error();
if (win32Error != 0)
throw new Win32Exception();
return fileStruct.FileId.QuadPart;
}
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool GetFileInformationByHandleEx(
IntPtr hFile,
FILE_INFO_BY_HANDLE_CLASS infoClass,
out FILE_ID_BOTH_DIR_INFO dirInfo,
uint dwBufferSize);
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern IntPtr CloseHandle(
IntPtr handle);
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern IntPtr CreateFile(
string lpFileName,
[MarshalAs(UnmanagedType.U4)] FileAccess dwDesiredAccess,
[MarshalAs(UnmanagedType.U4)] FileShare dwShareMode,
IntPtr lpSecurityAttributes,
[MarshalAs(UnmanagedType.U4)] FileMode dwCreationDisposition,
uint dwFlagsAndAttributes,
IntPtr hTemplateFile);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
private struct FILE_ID_BOTH_DIR_INFO
{
public uint NextEntryOffset;
public uint FileIndex;
public LARGE_INTEGER CreationTime;
public LARGE_INTEGER LastAccessTime;
public LARGE_INTEGER LastWriteTime;
public LARGE_INTEGER ChangeTime;
public LARGE_INTEGER EndOfFile;
public LARGE_INTEGER AllocationSize;
public uint FileAttributes;
public uint FileNameLength;
public uint EaSize;
public char ShortNameLength;
[MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 12)]
public string ShortName;
public LARGE_INTEGER FileId;
[MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 1)]
public string FileName;
}
[StructLayout(LayoutKind.Explicit, Size=8)]
private struct LARGE_INTEGER
{
[FieldOffset(0)]public Int64 QuadPart;
[FieldOffset(0)]public UInt32 LowPart;
[FieldOffset(4)]public Int32 HighPart;
}
private enum FILE_INFO_BY_HANDLE_CLASS
{
FileIdBothDirectoryInfo = 10
}
如果這不是問題,那麼這裏不應該存在。 – 2013-03-22 09:40:22
你可以問一個問題,然後發佈這個答案。使其他人更容易找到,這是SO的可接受格式。 – 2013-03-22 09:40:23
「如果有人需要解決方案,不是一個問題就是答案。」 - 你是什麼意思 ? – 2013-03-22 09:41:25