C# 运行cmd命令的代码

2018-07-20    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Diagnostics;
  
namespace ConsoleApplication1
{
    class command
    {
        public static string startcmd(string command)
        {
            string output = "";
            try
            {
  
                Process cmd = new Process();
                cmd.StartInfo.FileName = command;
  
                cmd.StartInfo.UseShellExecute = false; 
  
                cmd.StartInfo.RedirectStandardInput = true; 
                cmd.StartInfo.RedirectStandardOutput = true;
  
                cmd.StartInfo.CreateNoWindow = true;
                cmd.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
  
                cmd.Start(); 
  
                output = cmd.StandardOutput.ReadToEnd();
                Console.WriteLine(output);
                cmd.WaitForExit();
                cmd.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            return output;
        }
        public static string startcmd(string command, string argument)
        {
            string output = "";
            try
            {
                Process cmd = new Process();
  
                cmd.StartInfo.FileName = command;
                cmd.StartInfo.Arguments = argument;
  
                cmd.StartInfo.UseShellExecute = false; 
  
                cmd.StartInfo.RedirectStandardInput = true; 
                cmd.StartInfo.RedirectStandardOutput = true; 
  
                cmd.StartInfo.CreateNoWindow = true;
                cmd.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
  
                cmd.Start(); 
  
                output = cmd.StandardOutput.ReadToEnd();
                Console.WriteLine(output);
                cmd.WaitForExit();
                cmd.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            return output;
        }
    }
}
 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇:一个简单的抽奖转盘游戏

下一篇: php抓取网页图片