C#Directory.exist总是在本地网络上返回false

您所在的位置:网站首页 dir指令 C#Directory.exist总是在本地网络上返回false

C#Directory.exist总是在本地网络上返回false

#C#Directory.exist总是在本地网络上返回false| 来源: 网络整理| 查看: 265

这可能是一个与用户权限相关的问题。

从MSDN:

如果没有至少只读权限到 目录下有没有做,Exists方法将返回false。

如果您使用本地帐户而不是域帐户,则使用Directory.Exists()方法存在问题。

我在过去有类似的问题:我必须检查网络中是否存在网络共享,并且没有域。你的方式不适合我。最后,我放弃了Directory.Exists()方法,并最终使用NET USE命令(http://www.cezeo.com/tips-and-tricks/net-use-command/)

bool exists = false; string output = ""; string error = ""; System.Diagnostics.Process process = new System.Diagnostics.Process(); process = new System.Diagnostics.Process(); ExecuteShellCommand(process, "NET USE", "\""+ @path + "\" "+ this.password+ " /USER:"+machinename+"\\"+username + " /PERSISTENT:NO", ref output, ref error); Console.WriteLine("\r\n\t__________________________"+ "\r\n\tOutput:" + output.Trim().Replace("\r", " ") + "\r\n\tError: " + error.Trim().Replace("\r"," ")); if (output.Length>0 && error.Length==0) { exists = true; } process = new System.Diagnostics.Process(); ExecuteShellCommand(process, "NET USE", " /DELETE " + @path, ref output, ref error);

....

public void ExecuteShellCommand(System.Diagnostics.Process process, string fileToExecute, string command, ref string output, ref string error) { try { string CMD = string.Format(System.Globalization.CultureInfo.InvariantCulture, @"{0}\cmd.exe", new object[] { Environment.SystemDirectory }); string args = string.Format(System.Globalization.CultureInfo.InvariantCulture, "/C {0}", new object[] { fileToExecute }); if (command != null && command.Length > 0) { args += string.Format(System.Globalization.CultureInfo.InvariantCulture, " {0}", new object[] { command, System.Globalization.CultureInfo.InvariantCulture }); } System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(CMD, args); startInfo.CreateNoWindow = true; startInfo.UseShellExecute = false; startInfo.RedirectStandardOutput = true; startInfo.RedirectStandardInput = true; startInfo.RedirectStandardError = true; process.StartInfo = startInfo; process.Start(); // timeout process.WaitForExit(10 * 1000); output = process.StandardOutput.ReadToEnd(); error = process.StandardError.ReadToEnd(); } catch (Win32Exception e32) { Console.WriteLine("Win32 Exception caught in process: {0}", e32.ToString()); } catch (Exception e { Console.WriteLine("Exception caught in process: {0}", e.ToString()); } finally { // close process and do cleanup process.Close(); process.Dispose(); process = null; } }

我知道这是一个黑客,但它的工作对我而言,这是一种可能性。 (虽然你可能需要设置一个适当的净份额)



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3