【问题标题】:Get DFS folder target (local server path) from a DFS path in C#从 C# 中的 DFS 路径获取 DFS 文件夹目标(本地服务器路径)
【发布时间】:2018-07-24 08:22:55
【问题描述】:

我有一个 DFS 文件夹路径...

\\domain.name\SharesRoot\DFSShare

我需要获取此根链接的实际文件夹目标,或者换句话说,DFSShare 所针对的本地服务器路径

本地服务器路径/文件夹目标如下

\\MyServer\Share\MyShare

我已成功使用NetDfsGetClientInfo() 方法从给定的 DFS 路径中检索了一些所需信息

此方法填充的 DFS_STORAGE_INFO 对象为我提供了以下数据

  • 状态:无关数据
  • 服务器名称:“我的服务器”
  • ShareName: 'SharesRoot'

虽然 ServerName 是我正在寻找的,但 ShareName 对我没有任何帮助。

我需要找出路径是什么 MyServer DFS 路径 \\domain.name\SharesRoot\DFSShare 正在定位

NetDfsGetInfo() 给了我 1168 错误,所以该方法没有帮助,我还没有尝试 NetDfsEnum(),但是我的希望并不高,因为它填充了与 NetDfsGetClientInfo() 相同的结构。 . 如果有人在这里有任何其他线索,我将非常感激!

【问题讨论】:

    标签: c# winapi microsoft-distributed-file-system


    【解决方案1】:

    我发现在客户端上我需要先访问 DFS 路径,然后 Windows 中的某些东西会缓存它,所以当我执行 NetDfsGetClientInfo() 时,我会得到底层路径(而不是 dfs 路径)

    所以这对我有用:

    string dfsPath = "\\domain.name\SharesRoot\DFSShare";
    
    //by doing Directory.Exists we access the DFS path, causing Windows to resolve it
    if (Directory.Exists(dfsPath))
    {
        string dfsPath = DFS.GetSharePath(dfsPath);
        MessageBox.Show($"{dfsPath});
    }
    

    这是我在别处找到的课程代码(不是我的)

      class DFS
    {
        #region Import
        [DllImport("Netapi32.dll", EntryPoint = "NetApiBufferFree")]
        public static extern uint NetApiBufferFree(IntPtr Buffer);
    
        [DllImport("Netapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
        public static extern int NetDfsGetInfo(
            [MarshalAs(UnmanagedType.LPWStr)] string EntryPath,
            [MarshalAs(UnmanagedType.LPWStr)] string ServerName,
            [MarshalAs(UnmanagedType.LPWStr)] string ShareName,
            int Level,
            out IntPtr Buffer);
    
        [DllImport("Netapi32.dll")]
        public static extern int NetDfsGetClientInfo(
            [MarshalAs(UnmanagedType.LPWStr)] string EntryPath,
            [MarshalAs(UnmanagedType.LPWStr)] string ServerName,
            [MarshalAs(UnmanagedType.LPWStr)] string ShareName,
            int Level,
            out IntPtr Buffer);
    
        #endregion
    
        #region Structures
    
        public struct DFS_INFO_3
        {
            [MarshalAs(UnmanagedType.LPWStr)]
            public string EntryPath;
            [MarshalAs(UnmanagedType.LPWStr)]
            public string Comment;
            public UInt32 State;
            public UInt32 NumberOfStorages;
            public IntPtr Storages;
        }
    
        public struct DFS_STORAGE_INFO
        {
            public Int32 State;
            [MarshalAs(UnmanagedType.LPWStr)]
            public string ServerName;
            [MarshalAs(UnmanagedType.LPWStr)]
            public string ShareName;
        }
    
        #endregion
    
        const int DFS_VOLUME_STATE_OK = 0x00000001;
        const int DFS_VOLUME_STATE_ONLINE = 0x00000004;
        const int DFS_STORAGE_STATE_ONLINE = 0x00000002;
        const int DFS_STORAGE_STATE_ACTIVE = 0x00000004;
    
        public static String GetSharePath(String DFSPath)
        {
            if (!String.IsNullOrEmpty(DFSPath))
            {
                IntPtr Buffer = IntPtr.Zero;
                try
                {
                    Console.WriteLine("User : {0}, Domain= {1}", Environment.UserName, Environment.UserDomainName);
                    Console.WriteLine("Path is {0}", DFSPath);
                    int Error = NetDfsGetClientInfo(DFSPath, null, null, 3, out Buffer);
                    Console.WriteLine("Error code {0}", Error);
                    if (Error == 0)
                    {
                        DFS_INFO_3 DFSInfo = (DFS_INFO_3)Marshal.PtrToStructure(Buffer, typeof(DFS_INFO_3));
                        if ((DFSInfo.State & DFS_VOLUME_STATE_OK) > 0)
                        {
                            String SubPath = DFSPath.Remove(0, 1 + DFSInfo.EntryPath.Length).TrimStart(new Char[] { '\\' });
                            for (int i = 0; i < DFSInfo.NumberOfStorages; i++)
                            {
                                IntPtr Storage = new IntPtr(DFSInfo.Storages.ToInt64() + i * Marshal.SizeOf(typeof(DFS_STORAGE_INFO)));
                                DFS_STORAGE_INFO StorageInfo = (DFS_STORAGE_INFO)Marshal.PtrToStructure(Storage, typeof(DFS_STORAGE_INFO));
                                if ((StorageInfo.State & DFS_STORAGE_STATE_ACTIVE) > 0)
                                {
                                    if (String.IsNullOrEmpty(SubPath))
                                    {
                                        return String.Format(@"\\{0}\{1}", StorageInfo.ServerName, StorageInfo.ShareName);
                                    }
                                    else
                                    {
                                        return (String.Format(@"\\{0}\{1}\{2}", StorageInfo.ServerName, StorageInfo.ShareName, SubPath));
                                    }
                                }
                            }
                        }
                    }
                    else if (Error == 2662)
                        return DFSPath;
                }
                finally
                {
                    NetApiBufferFree(Buffer);
                }
            }
            return null;
        }
    
        public static String GetShareName(String SharePath)
        {
            if (!String.IsNullOrEmpty(SharePath))
            {
                String[] Tokens = SharePath.Trim(new Char[] { '\\' }).Split(new Char[] { '\\' }, StringSplitOptions.RemoveEmptyEntries);
                if (2 <= Tokens.Length)
                    return Tokens[1];
            }
            return null;
        }
    
    
    }
    

    【讨论】:

      【解决方案2】:

      看来NetDfsGetClientInfo() 确实是满足我特殊需求的答案。

      问题在于,它的行为会根据它是在 DFS 客户端计算机上调用还是在 DFS 服务器计算机上调用而发生变化。

      在客户端计算机上,该调用返回一个 DFS_STORAGE_INFO 对象,其中包含以下数据:

      • 服务器名称:“我的服务器”
      • 共享名称:“共享”

      一旦返回,所需要做的就是附加子路径,可以使用 DFS_INFO_3 对象的入口路径和 NetDfsGetClientInfo() 调用中使用的原始路径去除子路径

      【讨论】:

        猜你喜欢
        • 2016-01-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-07-21
        • 2012-11-27
        • 1970-01-01
        • 2019-02-13
        • 1970-01-01
        相关资源
        最近更新 更多