废话不多写,直接来干货。
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;
p.StartInfo.RedirectStandardInput =
true;
p.StartInfo.RedirectStandardOutput =
true;
p.StartInfo.RedirectStandardError =
true;
p.StartInfo.CreateNoWindow =
true;
p.Start();
p.StandardInput.WriteLine(cmdParament);
p.StandardInput.WriteLine(
"exit");
p.StandardInput.AutoFlush =
true;
string result = p.StandardOutput.ReadToEnd();
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);
}
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);
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());
}
}
}
}
转载请注明原文地址: https://ju.6miu.com/read-969371.html