我試圖追蹤文件和文件夾的整個生命週期(它們可能會被移動或重命名)。我已經完成搜索,發現FileSystemWatcher可能是最流行的跟蹤文件和文件夾的方式,但是,這對我來說並不適用,因爲應用程序可能會或可能不會一直運行。我選擇嘗試通過ID跟蹤文件夾。獲取文件夾ID C#
我找到了一種方法來跟蹤來自this stack post答案的ID中的文件。我可以根據this answer中的方法B成功獲取文件ID。
在搜索時,我發現this stack post表示他使用FSCTL_GET_OBJECT_ID找到了他的解決方案。我花了不少時間試圖弄清楚如何使用這個功能,但是我無法把頭圍住它。我基本上沒有從C#中調用本機窗口函數的經驗。
任何人都可以給我一個在這個正確的方向推?我覺得我必須失去一些明顯的東西。
是否有原因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的,這是根據MSDN上(我無法發佈由於聲譽的限制更多的鏈接後無效參數得到錯誤「87」後)
http://msdn.microsoft.com/en-us/library/windows/desktop/bb776914%28v=vs.85%29.aspx – MethodMan 2013-05-06 00:12:07
這是高檔功能服務器,具有自動故障轉移功能的羣集。它不會在您的開發機器的C:驅動器上工作。 – 2013-05-06 02:06:06
@HansPassant,請您詳細說明一下嗎?它在他所鏈接的信息源中工作,並且在我看到的API描述中沒有任何信息表明它不能在桌面上工作。 – 2013-05-06 02:13:57