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。不知如何獲取,謝謝!
{
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。不知如何獲取,謝謝!
string error = p.StandardError.ReadToEnd();用这个看看~~~~~
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));
{
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;
}