怎样将共享目录映射成盘符?

herman_chow 2003-08-25 03:32:17

现在想将另外一台电脑的一个共享目录映射成为本地的一个盘符,在C#里使用API函数WNetAddConnection2应该如何实现?

试过用WNetAddConnection可以成功映射,但连接的用户名必须和本地window用户名相同,而使用WNetAddConnection2则返回代码487(试图访问无效的地址。),不知是何原因?

除了使用net use之外是否还有什么方法可以实现映射?
...全文
588 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
herman_chow 2003-08-28
  • 打赏
  • 举报
回复

谢谢了!终于实现了!

马上给分!

另外想问一下:怎么在API函数里没有看到WNetAddConnection2A,只有WNetAddConnection、WNetAddConnection2和WNetAddConnection3呢?
_weiKun_ 2003-08-27
  • 打赏
  • 举报
回复
同意,楼上
TheAres 2003-08-27
  • 打赏
  • 举报
回复
using System;
using System.Runtime.InteropServices;

[StructLayout(LayoutKind.Sequential)]
public struct NETRESOURCEA
{
public int dwScope;
public int dwType;
public int dwDisplayType;
public int dwUsage;
[ MarshalAs (UnmanagedType.LPStr)]
public string lpLocalName;
[ MarshalAs (UnmanagedType.LPStr)]
public string lpRemoteName;
[ MarshalAs (UnmanagedType.LPStr)]
public string lpComment;
[ MarshalAs (UnmanagedType.LPStr)]
public string lpProvider;
public override String ToString()
{
String str = "LocalName: " + lpLocalName + " RemoteName: " + lpRemoteName
+ " Comment: " + lpComment + " lpProvider: " + lpProvider;
return(str);
}
}

class CMyMprTest
{
[DllImport("mpr.dll")]
public static extern int WNetAddConnection2A(
[MarshalAs(UnmanagedType.LPArray)] NETRESOURCEA[] lpNetResource,
[MarshalAs(UnmanagedType.LPStr)] string lpPassword,
[MarshalAs(UnmanagedType.LPStr)] string UserName,
int dwFlags);

public static void Main(String[] args)
{
NETRESOURCEA [] n = new NETRESOURCEA[1];
n[0] = new NETRESOURCEA();
n[0].dwType = 1;
int dwFlags = 1;
n[0].lpLocalName = @"X:";
n[0].lpRemoteName = @"\\antares\transfer";
n[0].lpProvider = null;
Console.WriteLine(n[0]);
int res = WNetAddConnection2A( n, null, null, dwFlags );
Console.WriteLine("WNetAddConnection3 returned : " + res);
Console.WriteLine(n[0]);
}
}
TheAres 2003-08-27
  • 打赏
  • 举报
回复
using System;
using System.Runtime.InteropServices;

[StructLayout(LayoutKind.Sequential)]
public struct NETRESOURCEA
{
public int dwScope;
public int dwType;
public int dwDisplayType;
public int dwUsage;
[ MarshalAs (UnmanagedType.LPStr)]
public string lpLocalName;
[ MarshalAs (UnmanagedType.LPStr)]
public string lpRemoteName;
[ MarshalAs (UnmanagedType.LPStr)]
public string lpComment;
[ MarshalAs (UnmanagedType.LPStr)]
public string lpProvider;
public override String ToString()
{
String str = "LocalName: " + lpLocalName + " RemoteName: " + lpRemoteName
+ " Comment: " + lpComment + " lpProvider: " + lpProvider;
return(str);
}
}

class CMyMprTest
{
[DllImport("mpr.dll")]
public static extern int WNetAddConnection2A(
[MarshalAs(UnmanagedType.LPArray)] NETRESOURCEA[] lpNetResource,
[MarshalAs(UnmanagedType.LPStr)] string lpPassword,
[MarshalAs(UnmanagedType.LPStr)] string UserName,
int dwFlags);

public static void Main(String[] args)
{
NETRESOURCEA [] n = new NETRESOURCEA[1];
n[0] = new NETRESOURCEA();
n[0].dwType = 1;
int dwFlags = 1;
n[0].lpLocalName = @"X:";
n[0].lpRemoteName = @"\\antares\transfer";
n[0].lpProvider = null;
Console.WriteLine(n[0]);
int res = WNetAddConnection2A( n, null, null, dwFlags );
Console.WriteLine("WNetAddConnection3 returned : " + res);
Console.WriteLine(n[0]);
}
}
herman_chow 2003-08-27
  • 打赏
  • 举报
回复

怎么没人理啊???
w915 2003-08-26
  • 打赏
  • 举报
回复
哈哈 没看清楚 见笑了 脸都红了 555~~
w915 2003-08-26
  • 打赏
  • 举报
回复
你在网上邻居里找到共享的目录,点右键“映射……”就可以了啊。
herman_chow 2003-08-26
  • 打赏
  • 举报
回复

有哪位高手可以帮帮忙啊?

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧