|
-
July 19th, 2011, 12:55 PM
#1
Mapped Drive to UNC
Using .NET 4
I'm trying to convert from Mapped Drive paths (e.g. "Z:\tmp\test.txt => \\server\tmp\test.txt") and I'm having mixed results.
I used a function from http://www.wiredprairie.us/blog/index.php/archives/22 that uses WNetGetConnection(http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx), which makes this arguably also a c++ question, and it worked the first time, but in subsequent uses it is returning error code 2250, which is ERROR_NO_NETWORK. I am very much still connected to the drive, and it worked minutes earlier, but no more.
Anyone have a better way or know what might be the issue?
Thanks!
Code for reference:
Code:
[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);
public static string DriveMapPathToUNC(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)
{
DirectoryInfo dir = new DirectoryInfo(originalPath);
string path = Path.GetFullPath(originalPath)
.Substring(Path.GetPathRoot(originalPath).Length);
return Path.Combine(sb.ToString().TrimEnd(), path);
}
}
}
return originalPath;
}
Last edited by Hoobs; July 19th, 2011 at 02:08 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|