c# - 如何以编程方式在 DFS 中获取事件的 UNC 路径

标签 c# wmi microsoft-distributed-file-system

给定一个 DFS 路径,我如何知道它当前以编程方式处于的事件路径是什么。

例如,我有 2 个服务器共享为 "\\Server1\Folder\""\\Server2\Folder\" 并且它打开了 DFS,所以它可以在 "\\DFS_Server\Folder\" 上访问,我怎么知道当前 "\\DFS_Server\Folder\" 上的事件路径是什么,是否是 "\\Server1\Folder\""\\Server2\Folder\"

最佳答案

如果我正确理解您的要求,还有一个 API 似乎可以满足您的需求:

// mscorlib (no additional assemblies needed)
using System.Runtime.InteropServices;

public static class Dfs
{
    private enum NetDfsInfoLevel
    {
        DfsInfo1 = 1,
        DfsInfo2 = 2,
        DfsInfo3 = 3,
        DfsInfo4 = 4,
        DfsInfo5 = 5,
        DfsInfo6 = 6,
        DfsInfo7 = 7,
        DfsInfo8 = 8,
        DfsInfo9 = 9,
        DfsInfo50 = 50,
        DfsInfo100 = 100,
        DfsInfo150 = 150,
    }

    [DllImport("netapi32.dll", SetLastError = true)]
    private static extern int NetApiBufferFree(IntPtr buffer);

    [DllImport("Netapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    private static extern int NetDfsGetInfo(
        [MarshalAs(UnmanagedType.LPWStr)] string DfsEntryPath, // DFS entry path for the volume
        [MarshalAs(UnmanagedType.LPWStr)] string ServerName,   // This parameter is currently ignored and should be NULL
        [MarshalAs(UnmanagedType.LPWStr)] string ShareName,    // This parameter is currently ignored and should be NULL.
        NetDfsInfoLevel Level,                                 // Level of information requested
        out IntPtr Buffer                                      // API allocates and returns buffer with requested info
        );

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
    private struct DFS_INFO_3
    {
        [MarshalAs(UnmanagedType.LPWStr)]
        public string EntryPath;
        [MarshalAs(UnmanagedType.LPWStr)]
        public string Comment;
        public int State;
        public int NumberOfStorages;
        public IntPtr Storage;
    }

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
    private struct DFS_STORAGE_INFO
    {
        public int State;
        [MarshalAs(UnmanagedType.LPWStr)]
        public string ServerName;
        [MarshalAs(UnmanagedType.LPWStr)]
        public string ShareName;
    }

    private static T GetStruct<T>(IntPtr buffer, int offset=0)where T:struct
    {
        T r = new T();
        r = (T) Marshal.PtrToStructure(buffer + offset * Marshal.SizeOf(r), typeof(T));
        return r;
    }

    public static string GetDfsInfo(string server)
    {
        string rval = null;
        IntPtr b;
        int r = NetDfsGetInfo(server, null, null, NetDfsInfoLevel.DfsInfo3, out b);
        if(r != 0)
        {
            NetApiBufferFree(b);

            // return passed string if not DFS
            return rval;
        }

        DFS_INFO_3 sRes = GetStruct<DFS_INFO_3>(b);
        if(sRes.NumberOfStorages > 0)
        {
            DFS_STORAGE_INFO sResInfo = GetStruct<DFS_STORAGE_INFO>(sRes.Storage);
            rval = string.Concat(@"\\", sResInfo.ServerName, @"\", sResInfo.ShareName, @"\");
        }

        NetApiBufferFree(b);

        return rval;
    }
}

像这样使用它:

string dfsPath = @"\\DFS_Server\Folder\";
string share = Dfs.GetDfsInfo(dfsPath)

有关 API 引用,请查看 NetDfsGetInfo 上的 msdn , DFS_INFO_3 , DFS_STORAGE_INFONetApiBufferFree .

关于c# - 如何以编程方式在 DFS 中获取事件的 UNC 路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3938669/

相关文章:

hadoop - 如何在hadoop dfs中检查群集的完整路径

java - 为什么这个 DFS 代码在某些情况下不起作用?

hadoop - 在 “hadoop fs -ls/”上获取异常

c# - EF Core 2.1 启动缓慢

c# - NPOI 支持 CSV/TSV?

C#改进算法

.net - 如何确定当前用户是否通过 .NET 中的终端服务登录?

c# - 如何更新现有的共享文件夹属性?

c# - 为什么Task Parallel Library在某些情况下调度任务会出现 'hidden' 1秒超时?

powershell - 将 Win32_OperatingSystem 的 CountryCode 转换为国家字符串