C# Directory.exist always return false on the local network

久未见 提交于 2019-12-05 20:34:59

It's probably an issue connected to user permissions.

From MSDN:

If you do not have at a minimum read-only permission to the directory, the Exists method will return false.

If you're using local account and not domain account, using Directory.Exists() method is problematic.

I had similar problem in the past: I had to check if a net share existed in my network and there was no domain. Your way didn't work for me. In the end, I gave up on Directory.Exists() method and ended up using NET USE command ( 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;
        }
    }

I know it's a hack but it worked for me and it's a possibility. (Although you may need to set up a proper net share)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!