c#process输⼊输出using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Diagnostics;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
exited{
Process p = new Process();
p.StartInfo.FileName = "format";
p.StartInfo.Arguments = " G: /FS:FAT /Q";
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;//true表⽰不显⽰⿊框,false表⽰显⽰dos界⾯
p.StartInfo.UseShellExecute = false;
p.EnableRaisingEvents = true;
p.Exited += new EventHandler(p_Exited);
p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);
p.ErrorDataReceived += new DataReceivedEventHandler(p_ErrorDataReceived);
p.Start();
p.StandardInput.WriteLine("");
p.StandardInput.WriteLine("");
//开始异步读取输出
p.BeginOutputReadLine();
p.BeginErrorReadLine();
//调⽤WaitForExit会等待Exited事件完成后再继续往下执⾏。
p.WaitForExit();
p.Close();
Console.WriteLine("exit");
}
void p_OutputDataReceived(Object sender, DataReceivedEventArgs e)
{
/
/这⾥是正常的输出
Console.WriteLine(e.Data);
}
void p_ErrorDataReceived(Object sender, DataReceivedEventArgs e)
{
//这⾥得到的是错误信息
Console.WriteLine(e.Data);
}
void p_Exited(Object sender, EventArgs e)
{
Console.WriteLine("finish");
}
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论