检查 .net 中的等效共享文件夹

发布于 2024-07-08 13:04:04 字数 209 浏览 7 评论 0 原文

在.net框架内有没有办法检查两个不同的共享文件夹是否实际上指向同一个物理目录? Windows 中的目录是否有某种唯一标识符? Google-fu 让我失望了。

(我的意思是,除了向一个文件写入一个临时文件并查看它是否出现在另一个文件中)

编辑:我想我已经发现了我需要的东西,感谢布罗迪让我在 System.Management 中指出了正确的方向命名空间。

Is there a way, within the .net framework, to check to see if two different shared folders are actually pointing to the same physical directory? Do directories in Windows have some sort of unique identifier? Google-fu is failing me.

(I mean, aside from writing a temp file to one and seeing if it appears in the other)

Edit: I think I've discovered what I need,with thanks to Brody for getting me pointed in the right direction in the System.Management namespace.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

忆悲凉 2024-07-15 13:04:04

如果您不使用 WMI,则非托管调用为 NetShareEnum 服务器名称为 NULL(本地计算机),级别为 502 以获得 SHARE_INFO_502 结构。 本地路径在shi502_path中。

P/Invoke 信息一如既往,在 pinvoke.net 结束。

If you don't go WMI, the unmanaged call is NetShareEnum with a servername of NULL (local computer) and a level of 502 to get a SHARE_INFO_502 struct. The local path is in shi502_path.

P/Invoke info, as always, is over at pinvoke.net.

千笙结 2024-07-15 13:04:04

您可以使用 System.Management 命名空间检查共享定义本身,但它并不容易使用。

它开始了类似的事情

ManagementClass management = new ManagementClass("\\\\.\\root\\cimv2", "Win32_Share", null)

,之后情况变得更糟。 我已经用它来创建共享。 希望您可以使用它来进行每个共享的路径并进行比较。

You can examine the share definition itself by using the System.Management namespace but it is not easy to use.

it starts something like

ManagementClass management = new ManagementClass("\\\\.\\root\\cimv2", "Win32_Share", null)

And it gets much worse after that. I have used it to create a share. Hopefully you can use it to the path for each share and compare.

独留℉清风醉 2024-07-15 13:04:04

我认为 .NET 框架没有提供比较 2 个目录所需的信息......
您必须采取不受管理的方法。 我就是这样做的:

class Program
{
    struct BY_HANDLE_FILE_INFORMATION
    {
        public uint FileAttributes;
        public System.Runtime.InteropServices.ComTypes.FILETIME CreationTime;
        public System.Runtime.InteropServices.ComTypes.FILETIME LastAccessTime;
        public System.Runtime.InteropServices.ComTypes.FILETIME LastWriteTime;
        public uint VolumeSerialNumber;
        public uint FileSizeHigh;
        public uint FileSizeLow;
        public uint NumberOfLinks;
        public uint FileIndexHigh;
        public uint FileIndexLow;
    }

    //
    // CreateFile constants
    //
    const uint FILE_SHARE_READ = 0x00000001;
    const uint FILE_SHARE_WRITE = 0x00000002;
    const uint FILE_SHARE_DELETE = 0x00000004;
    const uint OPEN_EXISTING = 3;

    const uint GENERIC_READ = (0x80000000);
    const uint GENERIC_WRITE = (0x40000000);

    const uint FILE_FLAG_NO_BUFFERING = 0x20000000;
    const uint FILE_READ_ATTRIBUTES = (0x0080);
    const uint FILE_WRITE_ATTRIBUTES = 0x0100;
    const uint ERROR_INSUFFICIENT_BUFFER = 122;
    const uint FILE_FLAG_BACKUP_SEMANTICS = 0x02000000;


    [DllImport("kernel32.dll", SetLastError = true)]
    static extern IntPtr CreateFile(
        string lpFileName,
        uint dwDesiredAccess,
        uint dwShareMode,
        IntPtr lpSecurityAttributes,
        uint dwCreationDisposition,
        uint dwFlagsAndAttributes,
        IntPtr hTemplateFile);

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

    static void Main(string[] args)
    {
        string dir1 = @"C:\MyTestDir";
        string dir2 = @"\\myMachine\MyTestDir";
        Console.WriteLine(CompareDirectories(dir1, dir2));
    }

    static bool CompareDirectories(string dir1, string dir2)
    {
        BY_HANDLE_FILE_INFORMATION fileInfo1, fileInfo2;
        IntPtr ptr1 = CreateFile(dir1, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING,  FILE_FLAG_BACKUP_SEMANTICS, IntPtr.Zero);
        if ((int)ptr1 == -1)
        {
            System.ComponentModel.Win32Exception t = new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error());
            Console.WriteLine(dir1 + ": " + t.Message);
            return false;
        }
        IntPtr ptr2 = CreateFile(dir2, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING,  FILE_FLAG_BACKUP_SEMANTICS, IntPtr.Zero);
        if ((int)ptr2 == -1)
        {
            System.ComponentModel.Win32Exception t = new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error());
            Console.WriteLine(dir2 + ": " + t.Message);
            return false;
        }
        GetFileInformationByHandle(ptr1, out fileInfo1);
        GetFileInformationByHandle(ptr2, out fileInfo2);

        return ((fileInfo1.FileIndexHigh == fileInfo2.FileIndexHigh) &&
            (fileInfo1.FileIndexLow == fileInfo2.FileIndexLow));
    }
}

有效! 希望这可以帮助。

干杯。

I think the .NET framework doesn't provide the information you need for comparing 2 directories ...
You have to take an unmanaged approach. This is how I did it:

class Program
{
    struct BY_HANDLE_FILE_INFORMATION
    {
        public uint FileAttributes;
        public System.Runtime.InteropServices.ComTypes.FILETIME CreationTime;
        public System.Runtime.InteropServices.ComTypes.FILETIME LastAccessTime;
        public System.Runtime.InteropServices.ComTypes.FILETIME LastWriteTime;
        public uint VolumeSerialNumber;
        public uint FileSizeHigh;
        public uint FileSizeLow;
        public uint NumberOfLinks;
        public uint FileIndexHigh;
        public uint FileIndexLow;
    }

    //
    // CreateFile constants
    //
    const uint FILE_SHARE_READ = 0x00000001;
    const uint FILE_SHARE_WRITE = 0x00000002;
    const uint FILE_SHARE_DELETE = 0x00000004;
    const uint OPEN_EXISTING = 3;

    const uint GENERIC_READ = (0x80000000);
    const uint GENERIC_WRITE = (0x40000000);

    const uint FILE_FLAG_NO_BUFFERING = 0x20000000;
    const uint FILE_READ_ATTRIBUTES = (0x0080);
    const uint FILE_WRITE_ATTRIBUTES = 0x0100;
    const uint ERROR_INSUFFICIENT_BUFFER = 122;
    const uint FILE_FLAG_BACKUP_SEMANTICS = 0x02000000;


    [DllImport("kernel32.dll", SetLastError = true)]
    static extern IntPtr CreateFile(
        string lpFileName,
        uint dwDesiredAccess,
        uint dwShareMode,
        IntPtr lpSecurityAttributes,
        uint dwCreationDisposition,
        uint dwFlagsAndAttributes,
        IntPtr hTemplateFile);

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

    static void Main(string[] args)
    {
        string dir1 = @"C:\MyTestDir";
        string dir2 = @"\\myMachine\MyTestDir";
        Console.WriteLine(CompareDirectories(dir1, dir2));
    }

    static bool CompareDirectories(string dir1, string dir2)
    {
        BY_HANDLE_FILE_INFORMATION fileInfo1, fileInfo2;
        IntPtr ptr1 = CreateFile(dir1, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING,  FILE_FLAG_BACKUP_SEMANTICS, IntPtr.Zero);
        if ((int)ptr1 == -1)
        {
            System.ComponentModel.Win32Exception t = new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error());
            Console.WriteLine(dir1 + ": " + t.Message);
            return false;
        }
        IntPtr ptr2 = CreateFile(dir2, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING,  FILE_FLAG_BACKUP_SEMANTICS, IntPtr.Zero);
        if ((int)ptr2 == -1)
        {
            System.ComponentModel.Win32Exception t = new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error());
            Console.WriteLine(dir2 + ": " + t.Message);
            return false;
        }
        GetFileInformationByHandle(ptr1, out fileInfo1);
        GetFileInformationByHandle(ptr2, out fileInfo2);

        return ((fileInfo1.FileIndexHigh == fileInfo2.FileIndexHigh) &&
            (fileInfo1.FileIndexLow == fileInfo2.FileIndexLow));
    }
}

It works! Hope this helps.

Cheers.

南笙 2024-07-15 13:04:04

我相信使用 WMI 查询将满足我需要做的事情:

Connection options = new ConnectionOptions();
ManagementScope scpoe = new ManagementScope("\\\\Server\\root\\cimv2", options);
ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_Share WHERE Name = '" + name +"'")

ManagementObjectSearcher searcher = new ManagementObjectSearch(scope, query);
ManagementObjectCollection qc = searcher.Get();

foreach (ManagementObject m in qc) {
    Console.WriteLine(m["Path"]);
}

并且 Path 属性将为我提供共享的物理路径,我可以用它来比较两个共享。

I believe using WMI queries will take care of what I need to do:

Connection options = new ConnectionOptions();
ManagementScope scpoe = new ManagementScope("\\\\Server\\root\\cimv2", options);
ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_Share WHERE Name = '" + name +"'")

ManagementObjectSearcher searcher = new ManagementObjectSearch(scope, query);
ManagementObjectCollection qc = searcher.Get();

foreach (ManagementObject m in qc) {
    Console.WriteLine(m["Path"]);
}

And the Path attribute will give me the physical path of the share, which I can use to compare the two shares.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文