private static string connectComp(string command)
        {
            Process p;
            p = new Process();
            p.StartInfo.FileName = "cmd.exe";            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.CreateNoWindow = true;            p.Start();
            p.StandardInput.WriteLine(command);
            p.StandardInput.WriteLine("exit");
            p.WaitForExit();
            string s = p.StandardOutput.ReadToEnd();
            p.Close();
            return s;
        }
        public static bool TestConnectSrc(string ipaddress1,string username,string password,out string msg)
        {
            msg = "";
            bool isok;
            string s = connectComp(string.Format(@"net use \\{0} {1} /user:{2}", new object[] { ipaddress1, password, username}));
            if (s.Contains("successfully"))
            {
                msg += "文件連接成功\r\n";
                isok =true;
            }
            else
            {
                msg += "文件連接失敗\r\n";
                isok =false;
            }
            connectComp(string.Format(@"net use \\{0} /delete",ipaddress1));            return isok;
        }因我的系統為中文,用下面的語句OK,
if (s.Contains("successfully"))
但是如果是中文的系統則返回的是包含"成功"。
現在需要得到net use .. 返回後的代碼,如1235。不知如何獲取,謝謝!

解决方案 »

  1.   

    net use 后面的代码不是你自己写的吗,知道啊
      

  2.   

    p.StartInfo.RedirectStandardError = true;
    string error = p.StandardError.ReadToEnd();用这个看看~~~~~
      

  3.   

    ^ō^ 环境变量: %errorlevel%==0 -> 表示成功..
      

  4.   

    connectComp(string.Format(@"net use \\{0} {1} /user:{2}", new object[] { ipaddress1, password, username}));
    if (connectComp(@"%errorlevel%"/* or: echo %errorlevel% */) == "0") //分别尝试两种方式..
    {
        msg += "文件連接成功\r\n"; isok = true;
    }
    else
    {
        msg += "文件連接失敗\r\n"; isok = false;
    }
    connectComp(string.Format(@"net use \\{0} /delete",ipaddress1));
      

  5.   

    尝试这样:private static string connectComp(string command)
    {
        Process p;
        p = new Process();
        p.StartInfo.FileName = "cmd.exe";    p.StartInfo.UseShellExecute = false;
        p.StartInfo.RedirectStandardInput = true;
        p.StartInfo.RedirectStandardOutput = true;
        p.StartInfo.CreateNoWindow = true;    p.Start();
        p.StandardInput.WriteLine(command);
        //p.StandardInput.WriteLine(@"cls");
        p.StandardInput.WriteLine(@"%errorlevel%"/* or: echo %errorlevel% */); //分别尝试两种方式..
        p.StandardInput.WriteLine("exit");
        p.WaitForExit();
        string s = p.StandardOutput.ReadToEnd();
        p.Close();
        return s;
    }public static bool TestConnectSrc(string ipaddress1,string username,string password,out string msg)
    {
        msg = "";
        bool isok;
        string s = connectComp(string.Format(@"net use \\{0} {1} /user:{2}", new object[] { ipaddress1, password, username}));    if (s.Contains("0")/* or: s == "0" */)
        {
            msg += "文件連接成功\r\n"; isok =true;
        }
        else
        {
            msg += "文件連接失敗\r\n"; isok =false;
        }    connectComp(string.Format(@"net use \\{0} /delete",ipaddress1));    return isok;
    }