我们尝试访问networking目录内的目录,但得到错误的结果(C#/ Windows):
var exists = Directory.Exists("Z:\Sessions\Data1");
“Z”是networking目录,“会话”是logging软件不断创build目录(例如“数据1”)并将一些数据放入其中的目录。 看来,WindowscachingData1错误的状态:该方法返回false。 但是,当我通过资源pipe理器访问目录,这是在这里。 当我用Explorer访问目录后运行方法(Directory.Exists)时,它返回true。 当然我可以保证这个目录在第一次尝试时确实存在。
这种行为的原因是什么? 我能做些什么呢?
编辑:看来,Windows无法连接到远程计算机的networking驱动器。 当我尝试浏览到资源pipe理器的目录时,它会自动尝试连接驱动器。
我可以在Windows XP或7上使用最新的.NET框架吗?
Windows服务中的关键事件处理C#
Windows – 将键盘键映射到与鼠标左键相同的操作
所以问题改变: 有没有办法强制窗户尝试通过.NET重新连接?
解决scheme: 重新连接断开的networking驱动器
创buildWindows Media编码器对象时,C#程序始终会崩溃
如何创build一个键在registry(Visual C#)?
检查第二个显示器是否以.NET连接(双显示器设置,笔记本电脑和扩展坞)
如何共享内存映射文件中的List值
如何以编程方式closures或打开“Windowsfunction”
我也使用一个远程驱动器,它需要几秒钟的时间来连接,所以我只是等待和工作每一次。 如果没有连接,它会发送一封电子邮件,我会检查它。
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.");
我不知道哪个版本的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);
然后:
IntPtr hWnd = new IntPtr(0); int res = WNetRestoreSingleConnection(hWnd,<your drive path>,false);
这种行为的原因是什么?
从文档引用:
如果您至少没有目录的只读权限,则Exists方法将返回false。
我能做些什么呢?
确保您正在以至少具有只读权限访问此文件夹的帐户运行.NET应用程序。 请注意,如果您正在ASP.NET应用程序中编写此代码,则可能不会如此,具体取决于您将Web服务器配置为在哪个帐户下执行您的应用程序,请采取必要的步骤为该帐户授予权限。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。