Created
December 2, 2016 20:33
-
-
Save SkyeHoefling/841f2f271ca6fda2230b7eace7775548 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[DllImport("mpr.dll", CharSet = CharSet.Auto)] | |
public static extern int WNetGetConnection( | |
[MarshalAs(UnmanagedType.LPTStr)] string localName, | |
[MarshalAs(UnmanagedType.LPTStr)] StringBuilder remoteName, | |
ref int length); | |
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 || error == 1201) | |
{ | |
DirectoryInfo dir = new DirectoryInfo(originalPath); | |
string path = Path.GetFullPath(originalPath) | |
.Substring(Path.GetPathRoot(originalPath).Length); | |
return Path.Combine(sb.ToString().TrimEnd(), path); | |
} | |
} | |
} | |
return originalPath; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment