因原程序是用cmd執行的,先打算使用WinForm界面來運行該程序(cmd還是在後台隱藏運行)
現有如下問題:
1.如何在程序運行期間一直隱藏開啟cmd?
2.如何能夠間端的發命令給cmd。並讀取cmd的顯示結果?
3.如何通過WinForm發送回車鍵給cmd?謝謝!
現有如下問題:
1.如何在程序運行期間一直隱藏開啟cmd?
2.如何能夠間端的發命令給cmd。並讀取cmd的顯示結果?
3.如何通過WinForm發送回車鍵給cmd?謝謝!
解决方案 »
- 无法将类型为“System.Windows.Forms.DataGridViewTextBoxCell”的对象强制转换为类型“System.Windows.Fo
- Interface 接口设计问题
- 我是学.NET的,现在想学SEO,不知道怎么样.
- 在C#的窗体中如何实现拖拽功能,详细请见内容
- 我想实现像csdn里的分页,如1,2,3,4,5,6...........在c#中如何实现
- 高手们,请教个高难度问题.放大图片的功能
- 局域网,winform下如何在知道客户端有几台机子在连接服务器.服务端就个数据库
- 文本是怎样选定的?
- 爱码啊!··你如此欺负我··
- BHO事件如何被js接收?
- C#MessageBox只出现一次的方法
- 关于Mdi父窗体的问题
{
Process 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();
this.textBox1.Text=textBox1.Text+ p.StandardOutput.ReadToEnd();
p.Close();
}這是我找到的代碼,可是好像只能運行一條命令。
我的需求是要隔一段時間運行一條命令,並根據cmd返回的成功信息後,再執行第2個命令。
{
tbResult.Text = "";
ProcessStartInfo start = new ProcessStartInfo("Ping.exe");//设置运行的命令行文件问ping.exe文件,这个文件系统会自己找到
//如果是其它exe文件,则有可能需要指定详细路径,如运行winRar.exe
start.Arguments = txtCommand.Text;//设置命令参数
start.CreateNoWindow = true;//不显示dos命令行窗口
start.RedirectStandardOutput = true;//
start.RedirectStandardInput = true;//
start.UseShellExecute = false;//是否指定操作系统外壳进程启动程序
Process p=Process.Start(start);
StreamReader reader = p.StandardOutput;//截取输出流
string line = reader.ReadLine();//每次读取一行
while (!reader.EndOfStream)
{
tbResult.AppendText(line+" ");
line = reader.ReadLine();
}
p.WaitForExit();//等待程序执行完退出进程
p.Close();//关闭进程
reader.Close();//关闭流
}
* Created by SharpDevelop.
* User: Administrator
* Date: 2008-5-10
* Time: 12:30
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;namespace WindowsApplication8
{
/// <summary>
/// Description of MainForm.
/// </summary>
public partial class MainForm : Form
{
public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
//
// TODO: Add constructor code after the InitializeComponent() call.
//
}
delegate void dReadLine(string strLine);
private void executeCommand(string strFile, string args, dReadLine onReadLine)
{
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo = new System.Diagnostics.ProcessStartInfo();
p.StartInfo.FileName = strFile;
p.StartInfo.Arguments = args;
p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.Start();
System.IO.StreamReader reader = p.StandardOutput;//截取输出流
string line = reader.ReadLine();//每次读取一行
while (!reader.EndOfStream)
{
onReadLine(line);
line = reader.ReadLine();
}
p.WaitForExit();
}
private void PrintMessage(string strLine)
{
this.textBox1.Text += "\r\n";
this.textBox1.Text += strLine ;
}
void Button1Click(object sender, EventArgs e)
{
executeCommand("ipconfig", "/all", new dReadLine(PrintMessage));
}
void MainFormLoad(object sender, EventArgs e)
{
this.textBox1.Multiline = true;
this.textBox1.WordWrap = false;
}
}
}
ProcessStartInfo.WindowStyle 属性
http://msdn.microsoft.com/zh-cn/vstudio/system.diagnostics.processstartinfo.windowstyle.aspx2,3
ProcessStartInfo.RedirectStandardInput 属性
ProcessStartInfo.RedirectStandardOutput 属性
*****************************************************************************
欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码) http://feiyun0112.cnblogs.com/
CreateProcess有参数可以设置,查MSDN就知道了2.如何能夠間端的發命令給cmd。並讀取cmd的顯示結果?
3.如何通過WinForm發送回車鍵給cmd?
使用管道,详情查MSDN
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 192.168.1.1");
p.StandardInput.WriteLine("exit");
string strRst = p.StandardOutput.ReadToEnd(); //读取返回的信息
其實我的cmd是調用另外一個軟件。該軟件運行後返回的結果也是顯示在cmd界面
2,3.需要用API, GetStdHandle,ReadConsoleOutput,WriteConsoleInput,GetConsoleScreenBufferInfo等来直接处理控制台缓冲区
謝謝周公!你的方法很好用。但相對我的程序有一點還需要改進,請幫忙看看怎麼改!我在發送一個命令給cmd後,它會處於等待狀態,此時我們需要按回車鍵才能繼續執行。
現在的問題有2個
1.在處於等待狀態時,WinForm界面就不能動,處於假死狀態,這應該如何處理?
2.如何發送回車鍵確認?
謝謝!
大家看看
如何用窗体的Load执行如ngen C:\mRelease\myinfoapp.exe的Vs2008的Dos命令呢?注意不是cmd.exe的
否则会出现ngen不是内部命令 !!!
現在的問題有2個
1.在處於等待狀態時,WinForm界面就不能動,處於假死狀態,這應該如何處理?
2.如何發送回車鍵確認?
謝謝!
使用windows API, GetStdHandle,ReadConsoleOutput,WriteConsoleInput,GetConsoleScreenBufferInfo等来直接处理控制台缓冲区,不会出现所谓的卡死问题