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!).
Asked
Active
Viewed 1,340 times
0
-
You'd have to have administrative privileges on the other PC and use something like WMI I guess. Or do you have a program running on the remote machine you can communicate with, which could determine that information locally? – PhilMasteG Aug 23 '15 at 13:20
-
I have a program running on the remote machine that needs to pick up the UNC sent to it (via TCP) and convert it to a local path. I'm basically creating a system that runs executables remotely. I haven't tested it out yet, but I might be able to feed my UNC path into Process.Execute directly on the remote PC (as the UNC path will always point to a file on the remote PC that it's sent to)? – Lee.J.Baxter Aug 23 '15 at 13:32
-
After playing around, I've found I can't use the UNC path to run the executable directly as the remote PC recognises it as a network folder (even though it's on the same PC) and confirms whether the user wishes to run it; this is unacceptable, as I need to run the executable without any need for confirmation. There must be some way to do this! – Lee.J.Baxter Aug 23 '15 at 16:48
-
Okay, I think I've found a way to do this using the registry on the remote PC; I'll post the code later on when I've completed it! – Lee.J.Baxter Aug 23 '15 at 20:27
1 Answers
0
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;
}

Lee.J.Baxter
- 495
- 4
- 12