簡體   English   中英

獲取文件夾ID C#

[英]Getting a Folder ID C#

我試圖在他們的生命周期中跟蹤文件和文件夾(他們可能被移動或重命名)。 我已經完成了搜索,發現FileSystemWatcher可能是最流行的跟蹤文件和文件夾的方式,但是,這對我來說不起作用,因為應用程序可能會或可能不會一直運行。 我選擇嘗試通過ID跟蹤文件夾。

我找到了一種方法來跟蹤來自此堆棧帖子的答案的ID中的文件。 我可以在此答案中基於方法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上的帖子無效參數(由於聲譽限制,我無法發布更多鏈接。)

似乎你對FileSystemWatcher沒有任何問題。 那么,對於如何在C#中使用DeviceIoControl ,看看這個答案:

物理磁盤大小不正確(IoCtlDiskGetDriveGeometry)

對於這個問題,它使用以下代碼完成:

class Program {
    const uint FSCTL_GET_OBJECT_ID=0x0009009c;

    public static String GetFileId(String path) {
        using(var fs=File.Open(
            path, 
            FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)
            ) {
            WinAPI.BY_HANDLE_FILE_INFORMATION info;
            WinAPI.GetFileInformationByHandle(fs.Handle, out info);
            return String.Format(
                    "{0:x}", ((info.FileIndexHigh<<32)|info.FileIndexLow));
        }
    }

    public static WinAPI.FILE_OBJECTID_BUFFER GetFolderIdBuffer(String path) {
        using(var hFile=WinAPI.CreateFile(
            path,
            WinAPI.GENERIC_READ, FileShare.Read,
            IntPtr.Zero,
            (FileMode)WinAPI.OPEN_EXISTING,
            WinAPI.FILE_FLAG_BACKUP_SEMANTICS,
            IntPtr.Zero
            )) {
            if(null==hFile||hFile.IsInvalid)
                throw new Win32Exception(Marshal.GetLastWin32Error());

            var buffer=default(WinAPI.FILE_OBJECTID_BUFFER);
            var nOutBufferSize=Marshal.SizeOf(buffer);
            var lpOutBuffer=Marshal.AllocHGlobal(nOutBufferSize);
            var lpBytesReturned=default(uint);

            var result=
                WinAPI.DeviceIoControl(
                    hFile, FSCTL_GET_OBJECT_ID,
                    IntPtr.Zero, 0,
                    lpOutBuffer, nOutBufferSize,
                    ref lpBytesReturned, IntPtr.Zero
                    );

            if(!result)
                throw new Win32Exception(Marshal.GetLastWin32Error());

            var type=typeof(WinAPI.FILE_OBJECTID_BUFFER);

            buffer=(WinAPI.FILE_OBJECTID_BUFFER)
                Marshal.PtrToStructure(lpOutBuffer, type);

            Marshal.FreeHGlobal(lpOutBuffer);
            return buffer;
        }
    }

    public static void Main(String[] args) {
        Console.WriteLine(
            GetFileId(@"C:\Users\Matt\Desktop\TestDocument.txt"));

        var buffer=GetFolderIdBuffer(@"C:\Users\Matt\Desktop");

        var objectId=buffer.ObjectId
                .Reverse()
                .Select(x => x.ToString("x2"))
                .Aggregate(String.Concat);

        Console.WriteLine("{0}", objectId);
    }
}

class WinAPI {
    internal const int
        GENERIC_READ=unchecked((int)0x80000000),
        FILE_FLAG_BACKUP_SEMANTICS=unchecked((int)0x02000000),
        OPEN_EXISTING=unchecked((int)3);

    [StructLayout(LayoutKind.Sequential)]
    public struct FILE_OBJECTID_BUFFER {
        public struct Union {
            [MarshalAs(UnmanagedType.ByValArray, SizeConst=16)]
            public byte[] BirthVolumeId;

            [MarshalAs(UnmanagedType.ByValArray, SizeConst=16)]
            public byte[] BirthObjectId;

            [MarshalAs(UnmanagedType.ByValArray, SizeConst=16)]
            public byte[] DomainId;
        }

        [MarshalAs(UnmanagedType.ByValArray, SizeConst=16)]
        public byte[] ObjectId;

        public Union BirthInfo;

        [MarshalAs(UnmanagedType.ByValArray, SizeConst=48)]
        public byte[] ExtendedInfo;
    }

    [StructLayout(LayoutKind.Sequential)]
    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;
    }

    [DllImport("kernel32.dll", SetLastError=true)]
    public static extern bool DeviceIoControl(
        SafeFileHandle hDevice,
        uint dwIoControlCode,
        IntPtr lpInBuffer,
        uint nInBufferSize,
        [Out] IntPtr lpOutBuffer,
        int nOutBufferSize,
        ref uint lpBytesReturned,
        IntPtr lpOverlapped
        );

    [DllImport("kernel32.dll", SetLastError=true)]
    public static extern SafeFileHandle CreateFile(
        String fileName,
        int dwDesiredAccess,
        System.IO.FileShare dwShareMode,
        IntPtr securityAttrs_MustBeZero,
        System.IO.FileMode dwCreationDisposition,
        int dwFlagsAndAttributes,
        IntPtr hTemplateFile_MustBeZero
        );

    [DllImport("kernel32.dll", SetLastError=true)]
    public static extern bool GetFileInformationByHandle(
        IntPtr hFile, out BY_HANDLE_FILE_INFORMATION lpFileInformation);
}

名稱空間需要使用:

using Microsoft.Win32.SafeHandles;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM