How do I determine a mapped drive's actual path?
So if I have a mapped drive on a machine called "Z", how can I use .NET to determine the machine and path for the mapped folder?
The code can assume it's running on the machine with the mapped drive.
I looked at Path
, Directory
, and FileInfo
objects, but can't seem to find anything.
I also looked for existing questions, but could not find what I'm looking for.
Here are some code samples:
All of the magic derives from a Windows function:
[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);
Example invocation:
var sb = new StringBuilder(512);
var size = sb.Capacity;
var error = Mpr.WNetGetConnection("Z:", sb, ref size);
if (error != 0)
throw new Win32Exception(error, "WNetGetConnection failed");
var networkpath = sb.ToString();