| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 743 人关注过本帖
标题:网络命令问题
只看楼主 加入收藏
随风云
Rank: 1
等 级:新手上路
威 望:1
帖 子:263
专家分:0
注 册:2007-6-28
收藏
 问题点数:0 回复次数:6 
网络命令问题

using System;
using System.Diagnostics;

namespace cmd
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: 在此处添加代码以启动应用程序
//
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.Start ();
p.StandardInput.WriteLine("ping -n 1 192.168.1.40");

p.StandardInput.WriteLine("exit");
string strRst = p.StandardOutput.ReadToEnd();

}
}
}
我尝试一下,但只弹出一个窗口后什么也没有了,而我有Ping查看又能看到结果,请高手指教!

搜索更多相关主题的帖子: 网络 summary 命令 using Process 
2007-06-30 09:36
swc
Rank: 3Rank: 3
等 级:论坛游民
威 望:6
帖 子:394
专家分:83
注 册:2006-4-7
收藏
得分:0 
p.StandardInput.WriteLine("exit");
你最后让它推出了。不要这句有没有结果?

实践、学习、再实践、再学习......
2007-06-30 11:24
随风云
Rank: 1
等 级:新手上路
威 望:1
帖 子:263
专家分:0
注 册:2007-6-28
收藏
得分:0 

试试再说


真的想象风一样去流浪!
2007-06-30 15:35
随风云
Rank: 1
等 级:新手上路
威 望:1
帖 子:263
专家分:0
注 册:2007-6-28
收藏
得分:0 
还是没有结果,显示一个cmd窗口,但输不进数据的

真的想象风一样去流浪!
2007-07-02 08:51
jy00318652
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2007-7-6
收藏
得分:0 

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Diagnostics;
namespace ChangeIP
{
///
/// Form1 的摘要说明。
///
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
///
/// 必需的设计器变量。
///
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

///
/// 清理所有正在使用的资源。
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
///
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
///
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(104, 96);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "ChangeIP";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);

}
#endregion

///
/// 应用程序的主入口点。
///
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void Form1_Load(object sender, System.EventArgs e)
{

}
private void abc()
{
string ip = "172.16.18.82";
string a="set address 本地连接 static "+ip+" 255.255.255.0";


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;
p.Start ();


p.StandardInput.WriteLine("netsh");
p.StandardInput.WriteLine("interface");
p.StandardInput.WriteLine("ip");
p.StandardInput.WriteLine(a.ToString ());
p.StandardInput.WriteLine("exit");
p.StandardInput.WriteLine("exit");
p.Close ();
MessageBox.Show ("111");


}

private void Test()
{


string ip = "172.16.18.1";


string pingrst;

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;

p.Start();

p.StandardInput.WriteLine("ping -n 1 "+ip);

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();

MessageBox.Show ( pingrst);

}

private void button1_Click(object sender, System.EventArgs e)
{
abc();
}

}

}

2007-07-19 13:31
立志成佛
Rank: 1
等 级:新手上路
威 望:2
帖 子:314
专家分:0
注 册:2006-11-1
收藏
得分:0 

其实是由结果的

[CODE] Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.UseShellExecute = false ;
p.Start();
p.StandardInput.WriteLine(@"ping 127.0.0.1");
p.StandardInput.WriteLine(@"exit");
this.richTextBox1.AppendText(p.StandardOutput .ReadToEnd().ToString());
[/CODE]

只不过它不是即时输出的,捕捉完输出后添加到一个TEXTBOX里就可以看到结果


曾经的曾经已不在
2007-07-19 16:17
随风云
Rank: 1
等 级:新手上路
威 望:1
帖 子:263
专家分:0
注 册:2007-6-28
收藏
得分:0 

谢谢!指教!


真的想象风一样去流浪!
2007-07-19 16:46
快速回复:网络命令问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012121 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved