使用WNetUseConnection连接到网络驱动器

使用WNetUseConnection连接到网络驱动器

本文介绍了使用WNetUseConnection连接到网络驱动器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在使用WNetUseConnection连接到网络驱动器,昨天一切正常,下面是我用来连接网络驱动器的代码。

I'm using WNetUseConnection to connect to a network drive, everything was working fine yesterday, below is the code I used to connect to the network drive.

NETRESOURCE nr = new NETRESOURCE();
nr.dwType = RESOURCETYPE_DISK;
nr.lpRemoteName = remote;
nr.lpLocalName = "L:";
int ret = WNetUseConnection(IntPtr.Zero, nr, username, password, 0, null, null, null);



我使用下面的代码找到远程的值

I find the value of remote using the below code

WNetGetConnection("L:", sb, ref size);
var remote= sb.ToString();



我使用以下代码取消连接

And I cancel the connection using the below code

WNetCancelConnection2("L:", 0, false); 



正如我之前提到的,昨天一切正常,但今天当我试图重新测试它失败的代码.WNetGetConnection在某些情况下返回错误1201而在其他情况下返回1202(注意驱动器是显示一个x符号,表示它已断开连接)

As I mentioned before everything worked fine yesterday, but today when I tried to retest the code it failed. The WNetGetConnection returned the error 1201 in some cases and 1202 in others (note that the drive was showing an x sign which means it was disconnected)


现在尝试使用WNetUseConnection重新连接到驱动器,有效凭据失败,错误1219这真的很奇怪,因为没有与驱动器建立的连接,同时尝试通过传递连接到驱动器空用户名
和密码工作正常。

Trying now to reconnect to the drive using WNetUseConnection and the valid credentials failed with error 1219 which was really weird since there was no established connections to the drive, meanwhile trying to connect to the drive with passing empty username and password work just fine.


使用WNetCancelConnection2取消连接工作并且连接成功取消但尝试再次执行WNetUseConnection以重新测试步骤会引发此错误67这是没有意义的,因为我的电脑仍在显示驱动器,但
,当我尝试双击驱动器时,它显示驱动器不存在的错误。

Using WNetCancelConnection2 to cancel the connection works and the connection is cancelled successfully but trying to execute WNetUseConnection again to retest the steps throws this error 67 which doesn't make sense since My PC is still showing the drive, but when I try to double click on the drive it shows me the error that the drive doesn't exist.


断开驱动器将无法正常工作,但将驱动器重新映射到具有相同驱动器号的相同位置并更换了网络驱动器。但重新映射并未解决问题。

Disconnect the drive won't work, but remapping the drive to the same location with the same drive letter worked and replaced the network drive. but remapping didn't solve the issue.


当我使用以下命令删除并重新创建驱动器时,一切正常。

When I deleted and recreated the drive using the below commands everything worked fine again.


net use l:"NetworkPath" / user:" username"" password"

net use l: "NetworkPath" /user:"username" "password"



有谁能告诉我为什么会这样?我很感激帮助。

Can anyone tells me why this happened? I appreciate the help.



编辑:


问题现在开始显现更频繁,我无法使用代码连接到驱动器,但是当我双击计算机上的驱动器图标时,连接成功创建。命令是不再解决问题。

The problem started to show more often now, I cannot connect to the drive using the code, but the connection is successfully created when I double click on the drive icon on my computer. The commands are no longer solving the issue.


PS:我使用的是Windows 10版本1709

PS: I'm using windows 10 version 1709


Ayat

Ayat

推荐答案

之所以重要,原因是如果Windows暂时处于非活动状态,Windows将与网络驱动器断开连接。这似乎是您与之交互时发生的情况你提到的Windows资源管理器。

The reason why that is important is that Windows will disconnect from a network drive if it is inactive for a while. That appears to be what is happening for your interactions with Windows Explorer that you mentioned.

然而,你得到的错误代码更具启发性。

The error codes you're getting are more revealing however.

67(ERROR_BAD_NET_NAME) - 远程路径不能是发现。这可能是一个间歇性的网络问题或者路径错误。

67 (ERROR_BAD_NET_NAME) - The remote path cannot be found. This can be an intermittent network issue or a bad path.

1201(ERROR_CONNECT_UNAVAIL) - 这表示该映射可用但空闲。 

1201 (ERROR_CONNECT_UNAVAIL) - This indicates the mapping is available but idled out. 

1219(ERROR_SESSION_CREDENTIAL_CONFLICT) - 这表示您尝试使用不同的凭据连接到同一UNC路径。 Windows不喜欢这样,并要求您删除任何未使用相同凭据的连接。如果你有连续的延迟,你会在资源管理器中定期看到

1219 (ERROR_SESSION_CREDENTIAL_CONFLICT) - This indicates you are trying to connect to the same UNC path with different credentials. Windows doesn't like this and will require that you drop any connections that aren't using the same credentials. You see this in Explorer periodically if you have lingering connections.

看来你的应用程序可能与该共享建立了连接,但从未放弃它。连接空闲,第二天尝试连接时失败。我建议您更新代码以使其更具弹性。我考虑将
包装成连接/断开逻辑,以便它始终配对。我还考虑检测常见错误(已连接等),并在这些情况下明确地丢弃连接并再次连接。

It appears that perhaps your app made a connection to the share and then never dropped it. The connection went idle and when you tried to connect the next day it failed. I would recommend updating your code to be a little more resilient. I'd consider wrapping the connect/disconnect logic so it always is paired. I'd also consider detecting the common errors (already connected, etc) and in those cases explicitly dropping the connection and connecting again.

我有一些代码,我在几年前工作,以支持.NET中的映射驱动器,但我从未完成它。不幸的是我没有把它检查到GitHub,但我可能仍然躺在那里,我将不得不看。在此期间,您可能会考虑使用第三个
派对库,甚至可能考虑使用WMI。这里有1个给使用WMI完成网络内容的人。鉴于WMI现状,我可能会尝试使用该路线而不是使用WNet功能。

I had some code I was working on years ago to support mapped drives in .NET but I never finished it. Unfortunately I didn't check it into GitHub either but I may still have it lying around, I'll have to look. In the meantime you might consider using a third party library instead or perhaps even WMI. Here's 1 link to someone who used WMI to accomplish the network stuff. Given the state of WMI now I'd probably try to go that route instead of using the WNet functionality.


这篇关于使用WNetUseConnection连接到网络驱动器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 07:18