在C#程序中,用WNetGetConnection来返回已经映射的网络驱动器盘符的UNC路径,但总是返回1200错误号。操作系统是Win2000+sp4,映射的目标UNC路径是完全的权限。
[DllImport("mpr.dll")]
public static extern int WNetGetConnection([MarshalAs(UnmanagedType.LPTStr)] string localName,
[MarshalAs(UnmanagedType.LPTStr)] System.Text.StringBuilder remoteName,
ref int length);
/// <summary>
/// 从映射的盘符中得到网络共享路径
/// </summary>
/// <param name="originalPath">映射的盘符</param>
/// <returns>如果成功,返回网络共享路径;否则返回空</returns>
public String GetUNCPath(string originalPath)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder(256);
Int32 size = sb.Capacity;
String UNCPath = "";
if (originalPath.Length >= 2 && originalPath[1] == ':')
{
char c = originalPath[0];
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
{
int error = WNetGetConnection(originalPath.Substring(0, 2), sb, ref size);
//这里的返回码总是1200
if (error == 0)
{
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(originalPath);
string path = System.IO.Path.GetFullPath(originalPath).Substring(System.IO.Path.GetPathRoot(originalPath).Length);
UNCPath = System.IO.Path.Combine(sb.ToString().TrimEnd(), path);
}
}
}
return UNCPath;
}
[DllImport("mpr.dll")]
public static extern int WNetGetConnection([MarshalAs(UnmanagedType.LPTStr)] string localName,
[MarshalAs(UnmanagedType.LPTStr)] System.Text.StringBuilder remoteName,
ref int length);
/// <summary>
/// 从映射的盘符中得到网络共享路径
/// </summary>
/// <param name="originalPath">映射的盘符</param>
/// <returns>如果成功,返回网络共享路径;否则返回空</returns>
public String GetUNCPath(string originalPath)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder(256);
Int32 size = sb.Capacity;
String UNCPath = "";
if (originalPath.Length >= 2 && originalPath[1] == ':')
{
char c = originalPath[0];
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
{
int error = WNetGetConnection(originalPath.Substring(0, 2), sb, ref size);
//这里的返回码总是1200
if (error == 0)
{
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(originalPath);
string path = System.IO.Path.GetFullPath(originalPath).Substring(System.IO.Path.GetPathRoot(originalPath).Length);
UNCPath = System.IO.Path.Combine(sb.ToString().TrimEnd(), path);
}
}
}
return UNCPath;
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货