.net 判断网络共享目录是否可以访问

using System;
using System.Diagnostics;

namespace ConsoleApplication23
{
    class Program
    {
        static void Main()
        {

            //判断网络共享目录是否可以访问
            Console.WriteLine(Cmd(@"net use \\192.168.32.145\ZhaoPinPrj"));
            
            Console.ReadKey();

        }

        static bool Cmd(string cmdLine)
        {

            using (var process = new Process
                {
                    StartInfo =
                        {
                            FileName = "cmd.exe",
                            UseShellExecute = false,
                            RedirectStandardInput = true,
                            RedirectStandardOutput = true,
                            CreateNoWindow = true,
                            RedirectStandardError = true
                        }
                })
            {
                process.Start();
                process.StandardInput.AutoFlush = true;
                process.StandardInput.WriteLine(cmdLine);
                process.StandardInput.WriteLine("exit");

                Debug.WriteLine(process.StandardOutput.ReadToEnd());

                String errorMessage = process.StandardError.ReadToEnd();
                process.WaitForExit();

                if (String.IsNullOrEmpty(errorMessage))
                {
                    return true;
                }

                Debug.WriteLine(errorMessage);

                return false;

            }
        }

    }
}

你可能感兴趣的:(.net/c#)