zoukankan      html  css  js  c++  java
  • 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
          }
    }

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

  • 相关阅读:
    Date类型 方法
    迭代方法和归并函数
    js快速排序方法
    reset
    水平垂直居中
    css清除浮动
    box-shadow
    display---我的第一篇博客
    centos7基础安装
    aws和ufile挂载数据盘EBS
  • 原文地址:https://www.cnblogs.com/yimai-series/p/15063093.html
Copyright © 2011-2022 走看看