Search This Blog

Friday, January 3, 2014

Get the Universal/Global Path/ Get the network path

This Class is used to fine the global path on network.
here is the code

class Pathing
    {
        [DllImport("mpr.dll", CharSet = CharSet.Unicode, SetLastError = true)]
        public static extern int WNetGetConnection(
            [MarshalAs(UnmanagedType.LPTStr)] string localName,
            [MarshalAs(UnmanagedType.LPTStr)] StringBuilder remoteName,
            ref int length);
        /// <summary>
        /// Given a path, returns the UNC path or the original.
        /// </summary>
        /// <param name="originalPath">The path to convert to a UNC Path</param>
        /// <returns>A UNC path. If a network drive letter is specified, the
        /// drive letter is converted to a UNC or network path. If the
        /// originalPath cannot be converted, it is returned unchanged.</returns>
        public static string GetUNCPath(string originalPath)
        {
            StringBuilder sb = new StringBuilder(512);
            int size = sb.Capacity;

            // look for the {LETTER}: combination ...
            if (originalPath.Length > 2 && originalPath[1] == ':')
            {
                // don't use char.IsLetter here - as that can be misleading
                // the only valid drive letters are a-z && A-Z.
                char c = originalPath[0];
                if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
                {
                    int error = WNetGetConnection(originalPath.Substring(0, 2),
                        sb, ref size);
                    if (error == 0)
                    {
                        DirectoryInfo dir = new DirectoryInfo(originalPath);
                        string path = Path.GetFullPath(originalPath)
                            .Substring(Path.GetPathRoot(originalPath).Length);
                        return Path.Combine(sb.ToString().TrimEnd(), path);
                    }
                }
            }

            return originalPath;
        }

        public static string GetUniversalName(string sFilePath)
        {
            if (sFilePath == string.Empty || sFilePath.IndexOf(":") > 1)
                return sFilePath;
            if (sFilePath.StartsWith("\\"))
            {
                return (new Uri(sFilePath)).ToString();
            }
            ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT RemoteName FROM win32_NetworkConnection WHERE LocalName = '" + sFilePath.Substring(0, 2) + "'");
            foreach (ManagementObject managementObject in searcher.Get())
            {
                string sRemoteName = managementObject["RemoteName"] as string;
                sRemoteName += sFilePath.Substring(2);
                //return (new Uri(sRemoteName)).ToString();
                return sRemoteName;
            }
            return sFilePath;
        }
    }

if you need to do some operation on network then use this class for finding the network path.

No comments:

Post a Comment