C#实现操作DOS命令的方法

使用是Visual studio 2019软件,Pragram.cs代码如下

using System;
using System.Collections;
using System.Diagnostics;
using System.Threading;
using DefExcel = Microsoft.Office.Interop.Excel;

namespace TestProcess
{
    class Program
    {
        static void Main(string[] args)
        {
            #region // 实现C#操作Dos命令
            Process p = new Process();
            p.StartInfo.FileName = "cmd.exe";   // 要执行的程序
            p.StartInfo.UseShellExecute = false;   // 不使用系统外壳程序启动
            p.StartInfo.RedirectStandardInput = false;   // 不重定向输入
            p.StartInfo.RedirectStandardOutput = true;   // 重定向输出
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.CreateNoWindow = true;      // 不创建窗口
            p.Start();           
            try
            {
                if (p.Start())//开始进程  
                {
                    p.StandardInput.WriteLine("tasklist");
                    p.StandardInput.WriteLine("exit");

                    string output = p.StandardOutput.ReadToEnd();//读取进程的输出  
                    Console.WriteLine($"{output}"); // 打印读取的内容
                }
            }
            catch
            {
                Console.WriteLine("出现异常");
            }
            finally
            {
                if (p != null)
                    p.Close();
            }
            #endregion
      }
}

*********************************** 完成 ***********************************************

原文地址:https://www.cnblogs.com/yimai-series/p/15063093.html