C#执行命令行

效果图

主要代码方法

 private Process p;
 public List<string> ExecuteCmd(string args)
 {
     System.Diagnostics.Process p = new System.Diagnostics.Process();
     p.StartInfo.FileName = "cmd.exe";
     p.StartInfo.RedirectStandardInput = true;
     p.StartInfo.RedirectStandardOutput = true;
     p.StartInfo.RedirectStandardError = true;
     p.StartInfo.CreateNoWindow = true;
     p.StartInfo.UseShellExecute = false;
     p.StartInfo.StandardOutputEncoding = System.Text.Encoding.UTF8;
     //p.StartInfo.StandardErrorEncoding = System.Text.Encoding.UTF8;
     p.Start();

     p.StandardInput.WriteLine(args + "&exit");

     p.StandardInput.AutoFlush = true;
     //p.StandardInput.WriteLine("exit");

     var list = new List<string>();
     StreamReader reader = p.StandardOutput;
     string line = reader.ReadLine();
     while (!reader.EndOfStream)
     {
         list.Add(line);
         line = reader.ReadLine();
     }

     p.WaitForExit();
     p.Close();
     return list;
 }

 

相关推荐

  1. C语言】命令

    2024-03-25 05:40:01       17 阅读
  2. c# 命令帮助类

    2024-03-25 05:40:01       22 阅读
  3. C语言】命令参数

    2024-03-25 05:40:01       9 阅读
  4. 【golang】go执行shell命令的方法( exec.Command )

    2024-03-25 05:40:01       45 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-25 05:40:01       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-25 05:40:01       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-25 05:40:01       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-25 05:40:01       18 阅读

热门阅读

  1. 进程和线程的区别

    2024-03-25 05:40:01       18 阅读
  2. 计算机视觉研究方向

    2024-03-25 05:40:01       18 阅读
  3. 设计模式面试专题

    2024-03-25 05:40:01       18 阅读
  4. Redis可以用作分布式共享session的解决方案

    2024-03-25 05:40:01       19 阅读
  5. c语言函数大全(I开头)

    2024-03-25 05:40:01       13 阅读
  6. 100 天机器学习指南

    2024-03-25 05:40:01       16 阅读