Paul blog

Explaining thoughts and findings is a great way to learn

How can we convert a local path to a UNC path in c#?

Uniform Naming Convention (UNC): According to wiki, UNC specifies a common syntax to describe the location of a network resource, such as a shared file, directory, or printer in Windows OS.

In client-server development, we  can send a server path to its clients as UNC path. Therefore, those clients can access resources from the server. The UNC syntax for Windows systems has the generic form: \\ComputerName\SharedFolder\Resource. However, the safe side is using IP instead of computer name like \\IP\SharedFolder\Resource. Here we are going to see how we can convert a local path (say d:\razan\\x.zip') to its UNC-name ( say '\\192.168.1.56 \razan\x.zip').

Why cannot we use string concatenation to do so?

The shared folder or drive can be shared as something other than the folder name/drive letter. Moreover, it is possible to have one-to-many mapping between local paths and UNC paths.

To implement this functionality, I have written the following class named UNCHelper.

   1: class UNCHelper
   2: {
   3:     public static string ConvertLocalFilePathsToUNCPath(string fileName, string sharedFolderpath)
   4:     {
   5:         string strPath = fileName.Substring(sharedFolderpath.Length);
   6:         string sharedFolderPathInUNC = ConvertLocalFolderPathToIPBasedUNCPath(sharedFolderpath);
   7:         string networkPath = sharedFolderPathInUNC + strPath;
   8:         return networkPath;
   9:     }
  10:  
  11:     public static string ConvertLocalFolderPathToIPBasedUNCPath(string localFolderName)
  12:     {
  13:         string ipBasedUNCPath = string.Empty;
  14:  
  15:         ManagementObjectSearcher managementObjectSearcher = new ManagementObjectSearcher("SELECT Name FROM Win32_share WHERE path ='" + localFolderName.Replace("\\", "\\\\") + "'");
  16:         ManagementObjectCollection managementObjectCollection = managementObjectSearcher.Get();
  17:         if (managementObjectCollection.Count != 0)
  18:         {
  19:             foreach (ManagementObject item in managementObjectCollection)
  20:             {
  21:                 String ComputerName = ReturnMachineIP().ToString();// use  Dns.GetHostName(); for computername instead of IP
  22:                 ipBasedUNCPath = item["Name"] as String;
  23:                 ipBasedUNCPath = "\\\\" + ComputerName + "\\" + ipBasedUNCPath;
  24:                 return ipBasedUNCPath;
  25:             }
  26:         }
  27:         return ipBasedUNCPath;
  28:     }
  29:  
  30:     private static IPAddress ReturnMachineIP()
  31:     {
  32:         String hostName = Dns.GetHostName();
  33:         IPHostEntry ipEntry = Dns.GetHostEntry(hostName);
  34:         IPAddress[] addr = ipEntry.AddressList;
  35:         IPAddress ipV4 = null;
  36:         foreach (IPAddress item in addr)
  37:         {
  38:             if (item.AddressFamily == AddressFamily.InterNetwork)
  39:             {
  40:                 ipV4 = item;
  41:                 break;
  42:             }
  43:  
  44:         }
  45:         if (ipV4 == null)
  46:         {
  47:             throw new ApplicationException("You have no IP of Version 4.Server can not run witout it");
  48:         }
  49:         return ipV4;
  50:     }     
  51: }

ManagementObjectSearcher is the class that is commonly used to query disk drives, network adapters, processes, shared folder and many more management objects on a system. To test the code you can use the following line:

   1: Console.WriteLine( UNCHelper.ConvertLocalFilePathsToUNCPath("F:\\clustershare\\ razan_3.evp","F:\\clustershare"));

Here clustershare is the shared folder name. It has been tested in Windows XP and Windows Vista.

Hope this will save some of your time.

Posted: 07-24-2009 9:27 PM by Razan | with no comments
Filed under: , ,