using System;class Car { public static void Main() { System.Console.Write("请输入:");//输入内容 string aa=(string)System.Console.ReadLine();//读取输入 int bb=Convert.ToInt32(aa); System.Console.WriteLine("你输入的是"+bb);
} } yes ?or not
static void Main(string[] args) { string a = args[0]; }
using System.Diagnostics; 实例一个Process类,启动一个独立进程 Process p = new Process(); Process类有一个StartInfo属性,这个是ProcessStartInfo类,包括了一些属性和方法, 下面我们用到了他的几个属性: 设定程序名 p.StartInfo.FileName = "cmd.exe"; 关闭Shell的使用 p.StartInfo.UseShellExecute = false; 重定向标准输入 p.StartInfo.RedirectStandardInput = true; 重定向标准输出 p.StartInfo.RedirectStandardOutput = true; 重定向错误输出 p.StartInfo.RedirectStandardError = true; 设置不显示窗口 p.StartInfo.CreateNoWindow = true; 上面几个属性的设置是比较关键的一步。 既然都设置好了那就启动进程吧, p.Start(); 输入要执行的命令,这里就是ping了, p.StandardInput.WriteLine("ping -n 1 192.192.132.229"); p.StandardInput.WriteLine("exit"); 从输出流获取命令执行结果, string strRst = p.StandardOutput.ReadToEnd();在本机测试得到如下结果: "Microsoft Windows 2000 [Version 5.00.2195]\r\n(C) 版权所有 1985-2000 Microsoft Corp.\r\n\r\nD:\\himuraz\\csharpproject\\ZZ\\ConsoleTest\\bin\\Debug>ping -n 1 192.192.132.231\r\n\r\r\nPinging 192.192.132.231 with 32 bytes of data:\r\r\n\r\r\nReply from 192.192.132.231: bytes=32 time<10ms TTL=128\r\r\n\r\r\nPing statistics for 192.192.132.231:\r\r\nPackets: Sent = 1, Received = 1, Lost = 0 (0% loss),\r\r\nApproximate round trip times in milli-seconds:\r\r\nMinimum = 0ms, Maximum =0ms, Average =0ms\r\r\n\r\nD:\\himuraz\\csharpproject\\ZZ\\ConsoleTest\\bin\\Debug>exit\r\n" 有了输出结果,那还有什么好说的,分析strRst字符串就可以知道网络的连接情况了。 下面是一个完整的程序,当然对Ping.exe程序执行的结果不全,读者可以进一步修改完整代码如下: using System; using System.Diagnostics;namespace ZZ { class ZZConsole { [STAThread] static void Main(string[] args) { string ip = "192.192.132.229"; string strRst = CmdPing(ip); Console.WriteLine(strRst); Console.ReadLine(); } private static string CmdPing(string strIp) { Process p = new Process(); p.StartInfo.FileName = "cmd.exe"; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardInput = true; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.RedirectStandardError = true; p.StartInfo.CreateNoWindow = true; string pingrst; p.Start(); p.StandardInput.WriteLine("ping -n 1 "+strIp); p.StandardInput.WriteLine("exit"); string strRst = p.StandardOutput.ReadToEnd(); if(strRst.IndexOf("(0% loss)")!=-1) pingrst = "连接"; else if( strRst.IndexOf("Destination host unreachable.")!=-1) pingrst = "无法到达目的主机"; else if(strRst.IndexOf("Request timed out.")!=-1) pingrst = "超时"; else if(strRst.IndexOf("Unknown host")!=-1) pingrst = "无法解析主机"; else pingrst = strRst; p.Close(); return pingrst; } } }
{
public static void Main()
{
System.Console.Write("请输入:");//输入内容
string aa=(string)System.Console.ReadLine();//读取输入
int bb=Convert.ToInt32(aa);
System.Console.WriteLine("你输入的是"+bb);
}
}
yes ?or not
{
string a = args[0];
}
实例一个Process类,启动一个独立进程
Process p = new Process();
Process类有一个StartInfo属性,这个是ProcessStartInfo类,包括了一些属性和方法,
下面我们用到了他的几个属性:
设定程序名
p.StartInfo.FileName = "cmd.exe";
关闭Shell的使用
p.StartInfo.UseShellExecute = false;
重定向标准输入
p.StartInfo.RedirectStandardInput = true;
重定向标准输出
p.StartInfo.RedirectStandardOutput = true;
重定向错误输出
p.StartInfo.RedirectStandardError = true;
设置不显示窗口
p.StartInfo.CreateNoWindow = true;
上面几个属性的设置是比较关键的一步。
既然都设置好了那就启动进程吧,
p.Start();
输入要执行的命令,这里就是ping了,
p.StandardInput.WriteLine("ping -n 1 192.192.132.229");
p.StandardInput.WriteLine("exit");
从输出流获取命令执行结果,
string strRst = p.StandardOutput.ReadToEnd();在本机测试得到如下结果:
"Microsoft Windows 2000 [Version 5.00.2195]\r\n(C) 版权所有 1985-2000 Microsoft Corp.\r\n\r\nD:\\himuraz\\csharpproject\\ZZ\\ConsoleTest\\bin\\Debug>ping -n 1 192.192.132.231\r\n\r\r\nPinging 192.192.132.231 with 32 bytes of data:\r\r\n\r\r\nReply from 192.192.132.231: bytes=32 time<10ms TTL=128\r\r\n\r\r\nPing statistics for 192.192.132.231:\r\r\nPackets: Sent = 1, Received = 1, Lost = 0 (0% loss),\r\r\nApproximate round trip times in milli-seconds:\r\r\nMinimum = 0ms, Maximum =0ms, Average =0ms\r\r\n\r\nD:\\himuraz\\csharpproject\\ZZ\\ConsoleTest\\bin\\Debug>exit\r\n"
有了输出结果,那还有什么好说的,分析strRst字符串就可以知道网络的连接情况了。 下面是一个完整的程序,当然对Ping.exe程序执行的结果不全,读者可以进一步修改完整代码如下:
using System;
using System.Diagnostics;namespace ZZ
{
class ZZConsole
{
[STAThread]
static void Main(string[] args)
{
string ip = "192.192.132.229";
string strRst = CmdPing(ip);
Console.WriteLine(strRst);
Console.ReadLine();
}
private static string CmdPing(string strIp)
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
string pingrst;
p.Start();
p.StandardInput.WriteLine("ping -n 1 "+strIp);
p.StandardInput.WriteLine("exit");
string strRst = p.StandardOutput.ReadToEnd();
if(strRst.IndexOf("(0% loss)")!=-1)
pingrst = "连接";
else if( strRst.IndexOf("Destination host unreachable.")!=-1)
pingrst = "无法到达目的主机";
else if(strRst.IndexOf("Request timed out.")!=-1)
pingrst = "超时";
else if(strRst.IndexOf("Unknown host")!=-1)
pingrst = "无法解析主机";
else
pingrst = strRst;
p.Close();
return pingrst;
}
}
}