C# 獲取cmd執行成功的代碼

bb_chen 2008-03-21 05:42:25
        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。不知如何獲取,謝謝!
...全文
213 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
shinaterry 2008-03-23
  • 打赏
  • 举报
回复
尝试这样:

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;
}
shinaterry 2008-03-23
  • 打赏
  • 举报
回复
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));
shinaterry 2008-03-23
  • 打赏
  • 举报
回复
^ō^ 环境变量: %errorlevel%==0 -> 表示成功..
bb_chen 2008-03-23
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 boblaw 的回复:]
if (s.Contains(1235.ToString())
[/Quote]

好象不對!
nik_Amis 2008-03-23
  • 打赏
  • 举报
回复
mark
lovefootball 2008-03-21
  • 打赏
  • 举报
回复
如果Error不为""就是有错误~~
lovefootball 2008-03-21
  • 打赏
  • 举报
回复
p.StartInfo.RedirectStandardError = true;


string error = p.StandardError.ReadToEnd();

用这个看看~~~~~
boblaw 2008-03-21
  • 打赏
  • 举报
回复
if (s.Contains(1235.ToString())
cpio 2008-03-21
  • 打赏
  • 举报
回复
net use 后面的代码不是你自己写的吗,知道啊

110,579

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧