.NET中的Process 类的简单使用
来源:互联网 发布:php和java哪个好学 编辑:程序博客网 时间:2024/05/21 09:29
废话不多写,直接来干货。
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Diagnostics;using System.Drawing;using System.IO;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace ProucessDemo{ public partial class 执行com命令 : Form { public 执行com命令() { InitializeComponent(); } private void 执行com命令_Load(object sender, EventArgs e) { } public string StartCmd(string cmdParament) { System.Diagnostics.Process p = new System.Diagnostics.Process(); p.StartInfo.FileName = "cmd.exe"; p.StartInfo.UseShellExecute = false; //是否使用操作系统shell启动 p.StartInfo.RedirectStandardInput = true;//接受来自调用程序的输入信息 p.StartInfo.RedirectStandardOutput = true;//由调用程序获取输出信息 p.StartInfo.RedirectStandardError = true;//重定向标准错误输出 p.StartInfo.CreateNoWindow = true;//不显示程序窗口 p.Start();//启动程序 //向cmd窗口发送输入信息 p.StandardInput.WriteLine(cmdParament); p.StandardInput.WriteLine("exit"); //向标准输入写入要执行的命令。这里使用&是批处理命令的符号,表示前面一个命令不管是否执行成功都执行后面(exit)命令,如果不执行exit命令,后面调用ReadToEnd()方法会假死 //同类的符号还有&&和||前者表示必须前一个命令执行成功才会执行后面的命令,后者表示必须前一个命令执行失败才会执行后面的命令 p.StandardInput.AutoFlush = true; //获取cmd窗口的输出信息 string result = p.StandardOutput.ReadToEnd(); //StreamReader reader = p.StandardOutput; //string line=reader.ReadLine(); //while (!reader.EndOfStream) //{ // str += line + " "; // line = reader.ReadLine(); //} p.WaitForExit();//等待程序执行完退出进程 p.Close(); return result; } private void button1_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(this.textBox2.Text.Trim())) { MessageBox.Show("命令框内容不能为空!"); return; } this.textBox1.Text = StartCmd(this.textBox2.Text); } /// <summary> /// 保存txt /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button2_Click(object sender, EventArgs e) { if(string.IsNullOrEmpty(this.textBox2.Text.Trim())){ MessageBox.Show("没有内容写入!"); return ; } this.saveFileDialog1.Filter = "文本文件(*.txt)|*.txt|xml文件(*.xml)|*.xml|word文档(*.docx)|*.docx"; if (this.saveFileDialog1.ShowDialog() == DialogResult.OK) { try { string FilePath = this.saveFileDialog1.FileName; string Content = this.textBox1.Text; File.WriteAllText(FilePath, Content, Encoding.UTF8); } catch (Exception ex) { MessageBox.Show(ex.Message); } } } #region 执行bat文件 private void 执行BatToolStripMenuItem_Click(object sender, EventArgs e) { if(this.openFileDialog1.ShowDialog()==DialogResult.OK){ if (string.IsNullOrEmpty(this.openFileDialog1.FileName)) { MessageBox.Show("选择内容为空"); return; } string FilePath = this.openFileDialog1.FileName; if (FilePath.Substring(FilePath.LastIndexOf(".")+1).Equals("bat")) { excuteCommand(FilePath, "", PrintMessage); } else { MessageBox.Show("请选择bat批处理文件!"); } } } delegate void dReadLine(string strLine); /// <summary> /// 执行BAT /// </summary> /// <param name="strFile"></param> /// <param name="args"></param> /// <param name="onReadLine"></param> private static void excuteCommand(string strFile, string args, dReadLine onReadLine) { try { 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(); } catch (Exception ex) { throw ex; } } private void PrintMessage(string strLine) { if (!"".Equals(strLine) && strLine != null) { this.textBox1.Text += strLine; } } #endregion private void textBox2_KeyDown(object sender, KeyEventArgs e) { if(e.KeyCode == Keys.Return) { button1_Click(button1, new EventArgs()); } } }}
0 0
- .NET中的Process 类的简单使用
- C#中的Process类使用
- C#中的Process类使用
- C#中的Process类使用
- process类的使用
- Process类的使用
- Process 类的使用
- process类的简单应用
- Process类的简单介绍
- java Process类的使用
- VB.NET Process类
- C#调动外部程序事例——Process类的简单使用
- 使用Process进行DOS命令交互的简单应用
- 使用Process进行DOS命令交互的简单应用
- android中使用process遇到的问题简单记录
- ASP.NET 使用Process类 发布 权限问题
- Java中的Process类
- Java中的Process类
- Request.getParameter()乱码问题
- redis学习笔记3(redis.conf)
- 30是什么意思
- 使用maven创建web项目
- 为什么我的AJAX请求处理成功了,还报404错误?
- .NET中的Process 类的简单使用
- 知识不清楚的问题汇总
- Android JNI + NDK 编程
- PAT B1046
- 【操作系统】实时调度
- POJ 1110 Double Vision 已被翻译
- rocketmq broker
- h5 手机屏幕适配—REM
- Hadoop学习笔记