返回顶部

收藏

C# 运行cmd命令的代码

更多
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;
        }
    }
}

标签:c#

收藏

0人收藏

支持

0

反对

0

发表评论