讨论c# 调用doc命令问题 现在我要用tracert命令跟踪一批域名的路由现在方案是每调用一次tracert命令创建一个线程每个线程产生了cmd.exe 和tracert.exe两个进程检测几百个域名,电脑一下子就变成了几百个进程问:有没有方法只开一个cmd.exe和tracert.exe循环检测几百个域名 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 将需要检测的所有域名存为一个字符串数组,创建一个线程启动cmd,然后一个一个顺序检测就可以了。 LS正解,要是还不明白就去百度“c#线程”吧 现在主要问题是怎么用c#循环输入命令到cmd.exe能否给个例子 问题解决了,下以是代码,给以后的人用private void GetAllInfo() { string pattern = @"(ms|\*).*(ms|\*).*(ms|\*).*"; string patternIp = @"\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}"; //验证IP Process proc = new Process(); proc.StartInfo.FileName = "cmd.exe"; proc.StartInfo.CreateNoWindow = true; proc.StartInfo.UseShellExecute = false; proc.StartInfo.RedirectStandardOutput = true; proc.StartInfo.RedirectStandardError = true; proc.StartInfo.RedirectStandardInput = true; //启动cmd命令 proc.Start(); StreamReader reader = null; //读取URL for (int i = 0; i < this.dgvData.Rows.Count; i++) { bool flag = false; int index = this.dgvData.Rows[i].Index; string url = this.dgvData.Rows[i].Cells["url"].Value.ToString(); string ip = this.dgvData.Rows[index].Cells["ip"].Value.ToString(); string result = this.dgvData.Rows[index].Cells["result"].Value.ToString(); this.dgvData.Rows[index].Cells["updatetime"].Value = string.Empty; //状态提示 int k = i + 1; UpdateState("正在检测第:" + k + " 个域名:" + url); proc.StandardInput.WriteLine("tracert -d -h 3 " + url); //proc.StandardInput.WriteLine("exit"); //这句要注释掉 reader = proc.StandardOutput; string line = reader.ReadLine(); while (!reader.EndOfStream) { if (flag == false) { if (Regex.IsMatch(line, pattern)) { Match mc = Regex.Match(line, patternIp); if (!GetIp().Contains(mc.Value)) { if (ip == mc.Value) { this.DgvDataInfo(index, mc.Value, result); //更新DataGridView窗体 } else { string turl = "http://www.ip138.com/ips.asp?ip=" + mc.Value + "&action=2"; string html = _search.GetRemoteHtmlCode(turl); html = _search.ReplaceEnter(html); html = _search.GetResult(html); this.DgvDataInfo(index, mc.Value, html); //更新DataGridView窗体 _search.Dispose(); } flag = true; } else if (line.IndexOf("Request") != -1) { this.DgvDataInfo(index, mc.Value, "Request timed out"); //更新DataGridView窗体 flag = true; } } } line = reader.ReadLine(); if (line.IndexOf("complete") != -1) { break; } } } UpdateState("执行完毕!"); proc.WaitForExit(); proc.Close(); reader.Close(); } C# 绘图为什么在窗体界面上显示不出来呢? 如何检验DATAGRIDVIEW里的数据变更了? 简单问题请教达人:为什么indexof不起作用? 求用C#实现生成PDF文档 怎么在winform中播放音乐 小弟新手,请问如何从文本文件中读取数据 windows Form 下datagrid绑定数组不显示数据的问题,有没有人有遇到过? 我的MSDN for VS2005是不是最新的? 【(分析区没人回答)请教 聊天机器人 回答 检索 算法】 有些B/S结构编辑Word的经验!共享、讨论! 大家看看这个电子白板 应该如何实现? 求解 关于IO操作
能否给个例子
private void GetAllInfo()
{
string pattern = @"(ms|\*).*(ms|\*).*(ms|\*).*";
string patternIp = @"\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}"; //验证IP Process proc = new Process();
proc.StartInfo.FileName = "cmd.exe";
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.RedirectStandardInput = true;
//启动cmd命令
proc.Start();
StreamReader reader = null;
//读取URL
for (int i = 0; i < this.dgvData.Rows.Count; i++)
{
bool flag = false;
int index = this.dgvData.Rows[i].Index;
string url = this.dgvData.Rows[i].Cells["url"].Value.ToString();
string ip = this.dgvData.Rows[index].Cells["ip"].Value.ToString();
string result = this.dgvData.Rows[index].Cells["result"].Value.ToString();
this.dgvData.Rows[index].Cells["updatetime"].Value = string.Empty;
//状态提示
int k = i + 1;
UpdateState("正在检测第:" + k + " 个域名:" + url); proc.StandardInput.WriteLine("tracert -d -h 3 " + url);
//proc.StandardInput.WriteLine("exit"); //这句要注释掉
reader = proc.StandardOutput;
string line = reader.ReadLine();
while (!reader.EndOfStream)
{
if (flag == false)
{
if (Regex.IsMatch(line, pattern))
{
Match mc = Regex.Match(line, patternIp);
if (!GetIp().Contains(mc.Value))
{
if (ip == mc.Value)
{
this.DgvDataInfo(index, mc.Value, result); //更新DataGridView窗体
}
else
{
string turl = "http://www.ip138.com/ips.asp?ip=" + mc.Value + "&action=2";
string html = _search.GetRemoteHtmlCode(turl);
html = _search.ReplaceEnter(html);
html = _search.GetResult(html);
this.DgvDataInfo(index, mc.Value, html); //更新DataGridView窗体
_search.Dispose();
}
flag = true;
}
else if (line.IndexOf("Request") != -1)
{
this.DgvDataInfo(index, mc.Value, "Request timed out"); //更新DataGridView窗体
flag = true;
}
} }
line = reader.ReadLine();
if (line.IndexOf("complete") != -1)
{
break;
}
}
}
UpdateState("执行完毕!");
proc.WaitForExit();
proc.Close();
reader.Close();
}