zqwuwei的技术博客

理论指导实践,在实践中更好的理解理论
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

C# Process类

Posted on 2013-04-03 16:51  zqwuwei  阅读(179)  评论(0编辑  收藏  举报
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            ProcessTest2();
        }

        static void ProcessTest2()
        { 
            var p = new Process();
            p.StartInfo.FileName = "cmd.exe";
            p.EnableRaisingEvents = true;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.UseShellExecute = false;
            p.Exited += new EventHandler(p_Exited);
            p.Start();
       // Visual Studio Command Prompt (2010) p.StandardInput.WriteLine(
@"%comspec% /k ""C:\Program Files\Microsoft Visual Studio 10.0\VC\vcvarsall.bat"" x86"); p.StandardInput.WriteLine("exit"); p.StandardInput.WriteLine("exit"); p.WaitForExit(); } static void ProcessTest1() { var p = new Process(); p.StartInfo.FileName = "cmd.exe"; p.EnableRaisingEvents = true; p.StartInfo.RedirectStandardInput = true; p.StartInfo.UseShellExecute = false; p.Exited += new EventHandler(p_Exited); p.Start(); p.StandardInput.WriteLine("del c:\\1.txt"); p.StandardInput.WriteLine("exit"); p.WaitForExit(); } static void p_Exited(object sender, EventArgs e) { using (StreamWriter sw = new StreamWriter("c:\\" + Guid.NewGuid().ToString() + ".txt")) { sw.Write(DateTime.Now); } Console.WriteLine("exit event"); } } }