How to use cmd with C#

    private string ExecuteCommand(string command)
    {
        ProcessStartInfo startInfo = new ProcessStartInfo() 
        {
            FileName = "cmd.exe",               
            UseShellExecute = false,
            CreateNoWindow = true,
            RedirectStandardInput = true,
            RedirectStandardOutput = true
        };

        var myProcess = Process.Start(startInfo);
        myProcess.StandardInput.WriteLine(command);
        myProcess.StandardInput.WriteLine("Exit");
        string output = myProcess.StandardOutput.ReadToEnd();        
        myProcess.Close();
        return output;
    }

 

posted on 2012-04-25 14:02  sanmao.net  阅读(160)  评论(0)    收藏  举报