无法使用 WNetAddConnection2 添加映射驱动器

发布于 2024-08-10 19:40:34 字数 1136 浏览 6 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执手闯天涯 2024-08-17 19:40:34

可能有帮助的 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)。例如,我通常用来映射驱动器的内容是:


With Res  
  .dwScope = RES_SCOPE_GLOBALNET 'value 2  
  .dwType = RES_TYPE_DISK 'value of 1  
  .dwUsage = RES_USE_CONNECT 'value of 1  
  .localName = "x:" 'leave blank for no drive   
  .RemoteName = "\\\"  
End With 
lRes = WNetAddConnection2(Res, sPassword, sDomain & "\" & sPassword, RES_CNN_UPDATE_PROFILE) 
If lRes = 0 Then
  'Success 
Else   
  'Error 
End If

Always check yourconnections before &从命令提示符调用后:

1a)从建立连接的系统,列出当前连接:

   net use

1b)从也连接的系统,列出当前会话:

   net session

要断开会话,请使用 API 'WNetCancelConnection2',我的代码如下:


sServer = "\\\"
lRes = WNetCancelConnection2(sServer, RES_CNN_UPDATE_PROFILE, True)
If lRes `> 0 Then 
  'Success
Else
  'Error
End If

或者,只需使用以下方式建立连接: “net”命令:

1) 映射驱动器盘符:

  net use `: \\`\` /user:`\` `

2) 映射 IPC 连接:

  net use \\`\` /user:`\` `

使用“net”命令断开连接:

1) 断开映射驱动器的连接:

  net use `: /delete

2) 断开服务器共享的连接:

  net use \\`\` /delete

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:


With Res  
  .dwScope = RES_SCOPE_GLOBALNET 'value 2  
  .dwType = RES_TYPE_DISK 'value of 1  
  .dwUsage = RES_USE_CONNECT 'value of 1  
  .localName = "x:" 'leave blank for no drive   
  .RemoteName = "\\\"  
End With 
lRes = WNetAddConnection2(Res, sPassword, sDomain & "\" & sPassword, RES_CNN_UPDATE_PROFILE) 
If lRes = 0 Then
  'Success 
Else   
  'Error 
End If

Always check your connections before & after calls from command prompt:

1a) From system making connection, list current connections:

   net use

1b) From system connected too, list current sessions:

   net session

To disconnect session, use API 'WNetCancelConnection2', my code following from above:


sServer = "\\\"
lRes = WNetCancelConnection2(sServer, RES_CNN_UPDATE_PROFILE, True)
If lRes `> 0 Then 
  'Success
Else
  'Error
End If

Alternatively, simply making connections using 'net' command:

1) To map a drive letter:

  net use `: \\`\` /user:`\` `

2) To map an IPC connection:

  net use \\`\` /user:`\` `

Disconnecting using 'net' command:

1) Disconnecting mapped drive:

  net use `: /delete

2) Disconnecting server share:

  net use \\`\` /delete
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文