.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;

            }
        }

    }
}

 

posted on 2013-04-25 14:39  HTTP500  阅读(275)  评论(0编辑  收藏  举报