无法使用 WNetAddConnection2 添加映射驱动器
我正在尝试使用 WNetAddCOnnection2
映射驱动器,但有些地方不太正确。我使用的代码来自 pinvoke.net ,似乎可以工作起初。如果我单步执行代码,我会得到 0 响应,并且我能够使用 System.IO.Directory.GetFiles() 来检查新的映射驱动器,这使我相信凭据是美好的。
问题是该驱动器在应用程序之外不可用。当我从命令提示符处输入 net use
时,我会看到如下列出的驱动器:
Unavailable L: \\<server>\<share> Microsoft Windows Network
当我尝试访问该驱动器时,我会得到:
The system cannot find the drive specified.
或
The system cannot find the path specified.
任何帮助将不胜感激。
下面是相关代码的概要:
NETRESOURCE res = new NETRESOURCE();
res.iScope = RESOURCE_GLOBALNET;
res.iType = RESOURCETYPE_DISK;
res.iDisplayType = RESOURCEDISPLAYTYPE_SHARE;
res.iUsage = RESOURCEUSAGE_CONNECTABLE;
res.sRemoteName = share;
res.sLocalName = drive;
res.sProvider = null;
int iFlags = 0;
iFlags = CONNECT_UPDATE_PROFILE;
int iResult = WNetAddConnection2( ref res, psPassword, psUsername, iFlags );
iResult
最终总是等于 0。
I'm trying to map a drive using WNetAddCOnnection2
but there's something not quite right. The code that I am using from pinvoke.net and seems to work at first. If I am stepping through the code I get a 0 for a response and I am able to use System.IO.Directory.GetFiles()
to inspect the new mapped drive which leads me to believe that credentials are fine.
The problem is that the drive is not available outside of the application. When I type net use
from a command prompt I see the drive listed like this:
Unavailable L: \\<server>\<share> Microsoft Windows Network
When I try to access the drive I get either:
The system cannot find the drive specified.
or
The system cannot find the path specified.
Any help would be greatly appreciated.
Here's the nutshell of the code in question:
NETRESOURCE res = new NETRESOURCE();
res.iScope = RESOURCE_GLOBALNET;
res.iType = RESOURCETYPE_DISK;
res.iDisplayType = RESOURCEDISPLAYTYPE_SHARE;
res.iUsage = RESOURCEUSAGE_CONNECTABLE;
res.sRemoteName = share;
res.sLocalName = drive;
res.sProvider = null;
int iFlags = 0;
iFlags = CONNECT_UPDATE_PROFILE;
int iResult = WNetAddConnection2( ref res, psPassword, psUsername, iFlags );
The iResult
always ends up equaling 0.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可能有帮助的 MSDN 文章:
* WNetAddConnection2 - [http://msdn.microsoft.com/en-us/library/aa385413%28VS.85%29.aspx][1]
* NETRESOURCE - [http://msdn.microsoft.com/en-us/library/aa385353%28VS.85%29.aspx][2]
我认为您的问题是显示类型,其中“res.iDisplayType = RESOURCEDISPLAYTYPE_SHARE”。也许尝试更改为“0”值(RESOURCEDISPLAYTYPE_GENERIC)。例如,我通常用来映射驱动器的内容是:
Always check yourconnections before &从命令提示符调用后:
1a)从建立连接的系统,列出当前连接:
1b)从也连接的系统,列出当前会话:
要断开会话,请使用 API 'WNetCancelConnection2',我的代码如下:
或者,只需使用以下方式建立连接: “net”命令:
1) 映射驱动器盘符:
2) 映射 IPC 连接:
使用“net”命令断开连接:
1) 断开映射驱动器的连接:
2) 断开服务器共享的连接:
MSDN articles that may assist:
* WNetAddConnection2 - [http://msdn.microsoft.com/en-us/library/aa385413%28VS.85%29.aspx][1]
* NETRESOURCE - [http://msdn.microsoft.com/en-us/library/aa385353%28VS.85%29.aspx][2]
I reckon your problem is the display type where "res.iDisplayType = RESOURCEDISPLAYTYPE_SHARE". Perhaps try changing to a value of "0" (RESOURCEDISPLAYTYPE_GENERIC). So for example, what I generally use to map drives appears:
Always check your connections before & after calls from command prompt:
1a) From system making connection, list current connections:
1b) From system connected too, list current sessions:
To disconnect session, use API 'WNetCancelConnection2', my code following from above:
Alternatively, simply making connections using 'net' command:
1) To map a drive letter:
2) To map an IPC connection:
Disconnecting using 'net' command:
1) Disconnecting mapped drive:
2) Disconnecting server share: