我正在尝试跟踪文件和文件夹的整个生命周期(它们可能被移动或重命名)。我已经完成了搜索,发现FileSystemWatcher可能是跟踪文件和文件夹的最流行的方式,但是,这对我不起作用,因为应用程序可能会或可能不会一直在运行。我选择尝试通过 ID 跟踪文件夹。
我找到了一种方法来从这个堆栈帖子的答案中的 ID 跟踪文件。我可以在这个答案中基于 Approach B 成功获取文件 ID 。
在搜索时,我发现了这个堆栈帖子,说明他使用FSCTL_GET_OBJECT_ID找到了他的解决方案。我花了很多时间试图弄清楚如何使用这个功能,但我无法理解它。我在 C# 中调用本机 Windows 函数的经验基本上为零。
谁能给我一个推动正确的方向呢?我觉得我一定遗漏了一些明显的东西。
C# 无法访问文件/文件夹 ID 是否有原因?跟踪文件/文件夹不常见吗?
编辑,添加代码:
static uint returnVal;
//Working example to get File ID
public static string GetFileId(string path)
{
WinAPI.BY_HANDLE_FILE_INFORMATION objectFileInfo = new WinAPI.BY_HANDLE_FILE_INFORMATION();
FileInfo fi = new FileInfo(path);
FileStream fs = fi.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
WinAPI.GetFileInformationByHandle(fs.Handle, out objectFileInfo);
fs.Close();
ulong fileIndex = ((ulong)objectFileInfo.FileIndexHigh << 32) + (ulong)objectFileInfo.FileIndexLow;
return fileIndex.ToString();
}
public static string GetFolderId(string path)
{
//Get a handle on the given folder
IntPtr cFile = WinAPI.CreateFile(
path,
WinAPI.GENERIC_READ,
FileShare.Read,
IntPtr.Zero,
(FileMode)WinAPI.OPEN_EXISTING,
WinAPI.FILE_FLAG_BACKUP_SEMANTICS,
IntPtr.Zero);
Console.WriteLine(path);
Console.WriteLine(cFile);
if ((int)cFile != -1)
{
int cFileSize = Marshal.SizeOf(typeof(IntPtr));
Console.WriteLine("cFile size = {0}", cFileSize);
IntPtr cFileBlob = Marshal.AllocHGlobal(cFileSize);
uint numBytesRead = 0;
WinAPI.DeviceIoControl(cFile, WinAPI.FSCTL_GET_OBJECT_ID, IntPtr.Zero, 0, cFileBlob, (uint)cFileSize, ref numBytesRead, IntPtr.Zero);
if (returnVal == 0)
{
Console.WriteLine(Marshal.GetLastWin32Error()); // Returning error 87 here
}
}
//Should be returning the ID from the folder.
return String.Empty;
}
public static void Main(string[] args)
{
Console.WriteLine(GetFileId(@"C:\Users\Matt\Desktop\TestDocument.txt"));
Console.WriteLine(GetFolderId(@"C:\Users\Matt\Desktop"));
}
}
class WinAPI
{
// Win32 constants for accessing files.
internal const int GENERIC_READ = unchecked((int)0x80000000);
internal const int FILE_FLAG_BACKUP_SEMANTICS = unchecked((int)0x02000000);
internal const int OPEN_EXISTING = unchecked((int)3);
internal const int FSCTL_GET_OBJECT_ID = 0x0009009c;
internal const int FSCTL_CREATE_OR_GET_OBJECT_ID = 0x000900c0;
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool DeviceIoControl(IntPtr hDevice, uint dwIoControlCode, IntPtr lpInBuffer, uint nInBufferSize, [Out] IntPtr lpOutBuffer, uint nOutBufferSize, ref uint lpBytesReturned, IntPtr lpOverlapped);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool GetFileInformationByHandle(IntPtr hFile, out BY_HANDLE_FILE_INFORMATION lpFileInformation);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern IntPtr CreateFile(
String fileName,
int dwDesiredAccess,
System.IO.FileShare dwShareMode,
IntPtr securityAttrs_MustBeZero,
System.IO.FileMode dwCreationDisposition,
int dwFlagsAndAttributes,
IntPtr hTemplateFile_MustBeZero);
public struct BY_HANDLE_FILE_INFORMATION
{
public uint FileAttributes;
public FILETIME CreationTime;
public FILETIME LastAccessTime;
public FILETIME LastWriteTime;
public uint VolumeSerialNumber;
public uint FileSizeHigh;
public uint FileSizeLow;
public uint NumberOfLinks;
public uint FileIndexHigh;
public uint FileIndexLow;
}
}
我从 DeviceIoControl 收到错误“87”,根据 MSDN 上的帖子,这是无效的参数(由于声誉限制,我无法发布更多链接。)