如何强制 Windows 重新连接到网络驱动器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8629760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to force Windows to reconnect to network drive
提问by nepa
We try to access a directory which is within a network directory but get wrong results (C#/Windows):
我们尝试访问网络目录中的目录但得到错误结果(C#/Windows):
var exists = Directory.Exists("Z:\Sessions\Data1");
"Z" is the network directory, "Sessions" is a directory where a recording software constantly creates directories (e.g. "Data1") and puts some data in it. It seems that Windows caches the wrong state about Data1: The method returns false. But when I access the directory via Explorer it's here. When I run the method (Directory.Exists) after accessing the directory with Explorer, it returns true. Of course I can guarantee that the directory actually exists at the first attempt.
“Z”是网络目录,“Sessions”是录音软件不断创建目录(例如“Data1”)并将一些数据放入其中的目录。似乎 Windows 缓存了关于 Data1 的错误状态:该方法返回 false。但是当我通过资源管理器访问目录时,它就在这里。当我在使用资源管理器访问目录后运行方法 (Directory.Exists) 时,它返回 true。当然,我可以保证第一次尝试时该目录确实存在。
What is the reason for this behaviour? What can I do about it?
这种行为的原因是什么?我该怎么办?
Edit:It seems that windows could not connect the network drive to the remote computer. When I try to navigate into the directory with Explorer it automatically tries to connect the drive.
编辑:似乎 windows 无法将网络驱动器连接到远程计算机。当我尝试使用资源管理器导航到目录时,它会自动尝试连接驱动器。
So the question changes: Is there a way to force windows to try a reconnect via .NET?
所以问题发生了变化: 有没有办法强制 Windows 尝试通过 .NET 重新连接?
Solution:Reconnecting a disconnected network drive
解决方案:重新连接断开的网络驱动器
回答by Jess
I'm not sure which version of mpr.dll the solution link above works with, but I am using Win7 and have a slightly different version (although similar). This entry point is:
我不确定上面的解决方案链接适用于哪个版本的 mpr.dll,但我使用的是 Win7 并且版本略有不同(尽管相似)。这个入口点是:
[DllImport("mpr.dll", SetLastError = true, EntryPoint = "WNetRestoreSingleConnectionW", CharSet = CharSet.Unicode)]
internal static extern int WNetRestoreSingleConnection( IntPtr windowHandle,
[MarshalAs(UnmanagedType.LPWStr)] string localDrive,
[MarshalAs(UnmanagedType.Bool)] bool useUI);
then:
然后:
IntPtr hWnd = new IntPtr(0);
int res = WNetRestoreSingleConnection(hWnd, <your drive path>, false);
You'll have to add your own error checking / handling.
您必须添加自己的错误检查/处理。
回答by Roger Deep
I too use a remote drive and it takes a few seconds to connect, so I just wait and works every time. If it does not connect, it will send an email and I will check it.
我也使用远程驱动器,连接需要几秒钟,所以我每次都等待并工作。如果它没有连接,它会发送一封电子邮件,我会检查它。
logger.Info("Create Z: drive ");
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = @"/C net use z: \" + Tools.servername + @"\SHARE_NAME_HERE /user:USER_NAME_HERE PASSWORD_HERE";
process.StartInfo = startInfo;
process.Start();
logger.Info("Z: drive created ");
// wait get Z:
int xtimestimeout = 5;
while (!Directory.Exists(@"Z:\") & (xtimestimeout > 0))
{
Application.DoEvents();
SetBalloonTip(@"Program", @"connecting... please wait");
ShowBalloon();
logger.Info("Creating Z:... waiting...");
Application.DoEvents();
System.Threading.Thread.Sleep(3000);
xtimestimeout -= 1;
}
// check for sucessfull creation of Z: in server Z:\somedirectory
if (!Directory.Exists(@"Z:\"))
{
SendEmail2("Oh my... help!", "drive Z: not created <<< CHECK!");
logger.Info("Z: email sent because of not created");
}
logger.Info("Z: drive created successfully.");
回答by Darin Dimitrov
What is the reason for this behaviour?
这种行为的原因是什么?
Quote from the documentation:
从文档中引用:
If you do not have at a minimum read-only permission to the directory, the Exists method will return false.
如果您没有对该目录的最低只读权限,Exists 方法将返回 false。
What can I do about it?
我该怎么办?
Ensure that you are running your .NET application under an account that has at least read-only permission to access this folder. Notice that if you are writing this in an ASP.NET application this probably won't be the case so depending on which account you configured your web server to execute your application under, take the necessary steps to grant permissions to this account.
确保在至少具有访问此文件夹的只读权限的帐户下运行 .NET 应用程序。请注意,如果您在 ASP.NET 应用程序中编写此代码,则可能不会出现这种情况,因此根据您将 Web 服务器配置为在哪个帐户下执行应用程序,请采取必要的步骤向该帐户授予权限。