如何将 UNC 路径转换回远程 PC 上的绝对本地路径? [英] How do I convert a UNC path back to an absolute local path on the remote PC?

查看:58
本文介绍了如何将 UNC 路径转换回远程 PC 上的绝对本地路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一种将 UNC 路径转换为远程 PC 上的本地路径的方法.因此,例如,我可以有一个 UNC 路径\\PC2\SharedFolder\Foo\Bar.exe",它指向 PC2 上的C:\SomeFolder\Foo\Bar.exe";后者是我要返回的(我要返回共享的路径,而不是映射驱动器!).

I need a method of converting a UNC path into a local path on the remote PC. So, for example, I could have a UNC path "\\PC2\SharedFolder\Foo\Bar.exe", which points to "C:\SomeFolder\Foo\Bar.exe" on PC2; the latter is what I want to return (I want to return the path of the share, not a mapped drive!).

推荐答案

好的,我已将代码发布到下面的解决方案中!请注意,我还没有完全测试过它,但到目前为止它似乎可以工作!!!

Okay, I've posted the code to my solution below! Note that I haven't fully tested it yet, but it seems to work so far!!!

    /// <summary>
    /// Gets whether the specified path is a UNC path or not.
    /// </summary>
    /// <param name="path">the path.</param>
    /// <returns>true if the path is a UNC path, otherwise false.</returns>
    public static bool IsUNCPath(
        string path)
    {
        try { return (new Uri(path)).IsUnc; }
        catch { return false; }
    }

    /// <summary>
    /// Gets a dictionary containing the mappings of each share's UNC path to its physical path on this PC.
    /// </summary>
    /// <returns>the dictionary containing the mappings.</returns>
    public static Dictionary<string, string> GetShareUNCPathToPhysicalPathMappings()
    {
        // Create a blank dictionary to hold the mappings.
        Dictionary<string, string> mappings = new Dictionary<string, string>();

        // Get this PC's host name.
        string hostName = Dns.GetHostName();

        // Get the registry key that contains the share information.
        using (RegistryKey shareKey = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Services\LanmanServer\Shares"))
        {
            // If the registry key isn't null...
            if (shareKey != null)
            {
                // Get the share names and go through each one...
                string[] shareNames = shareKey.GetValueNames();
                foreach (string shareName in shareNames)
                {
                    // Get the properties for the share and go through each one.
                    string[] shareProperties = (string[])shareKey.GetValue(shareName);
                    foreach (string shareProperty in shareProperties)
                    {
                        // Find the path property for the share and create the mapping.
                        if (shareProperty.StartsWith("Path="))
                        {
                            mappings[string.Format(@"\\{0}\{1}\", hostName, shareName)] = shareProperty.Remove(0, 5) + @"\";
                            break;
                        }
                    }
                }
            }
        }

        // Return the mappings.
        return mappings;
    }

    /// <summary>
    /// Converts a UNC path to a physical path (note: the UNC path must correspond to a physical path on this PC).
    /// </summary>
    /// <param name="uncPath">the UNC path.</param>
    /// <returns>the physical path, or null if the UNC path doesn't correspond to a physical path on this PC.</returns>
    public static string ConvertUNCPathToPhysicalPath(
        string uncPath)
    {
        // If the supplied path isn't a UNC path, return null.
        if (!IsUNCPath(uncPath)) return null;

        // Attempt to find the physical path that the UNC path corresponds to.
        Dictionary<string, string> mappings = GetShareUNCPathToPhysicalPathMappings();
        foreach (string shareUNCPath in mappings.Keys)
        {
            if (uncPath.StartsWith(shareUNCPath))
            {
                return mappings[shareUNCPath] + uncPath.Remove(0, shareUNCPath.Length);
            }
        }

        // If no mapping could be found, return null.
        return null;
    }

这篇关于如何将 UNC 路径转换回远程 PC 上的绝对本地路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆