Reverse mapping of drive letter to server address

You need the pure server address of a remote directory mapped to a drive letter? Try this:

[DllImport("Mpr.dll", CharSet = CharSet.Auto)]
private static extern int WNetGetConnection(
    [MarshalAs(UnmanagedType.LPTStr)] string localName,
    [MarshalAs(UnmanagedType.LPTStr)] StringBuilder remoteName,
    ref int length);

public static String GetServerPath(char driverLetter)
        int length = 1024;
        StringBuilder result = new StringBuilder(length);
        WNetGetConnection(driverLetter + ":", result, ref length);

        return result.ToString();
    return null;

public static String GetServerPath(String filename)
    if (filename.Length > 1 && filename[1] == ':')
        String serverpath = GetServerPath(filename[0]);
        if (serverpath != null && serverpath.Trim().Length > 0)
            filename = serverpath + filename.Substring(2);
    return filename;

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this:
search previous next tag category expand menu location phone mail time cart zoom edit close