C# - 使用包含%VS110COMNTOOLS%的参数执行CMD.exe

时间:2015-02-20 08:30:17

标签: winforms visual-studio-2012

我一直在尝试从C#在cmd.exe上执行下面提到的命令。我尝试了下面提到的方法,但它们似乎都没有用。他们都能够打开CMD,但之后没有任何事情发生。此外,如果我使用简单的参数,如“/ C start winword”,它只适用于方法3。

此外,我已经尝试将/ C和/ K添加到argumentstring但它们似乎不能正常工作。

        string argumentString = "\"%VS110COMNTOOLS%/../IDE/devenv.exe\" /diff " + "\"" + txt_File1.Text + "\" \"" + txt_file2.Text + "\"";
        //string argumentString = "start winword";

        // Method 1 
        System.Diagnostics.Process ExecuteCommand = new System.Diagnostics.Process();
        System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
        startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
        startInfo.FileName = "cmd.exe";
        startInfo.UseShellExecute = true;

        startInfo.Arguments = argumentString;
        ExecuteCommand.StartInfo = startInfo;
        ExecuteCommand.Start();

        //Method 2
        System.Diagnostics.Process.Start(@"cmd.exe", argumentString);

        // Method 3
        System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", argumentString);
        procStartInfo.RedirectStandardOutput = true;
        procStartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
        procStartInfo.UseShellExecute = false;
        procStartInfo.CreateNoWindow = false;
        System.Diagnostics.Process proc = new System.Diagnostics.Process();
        proc.StartInfo = procStartInfo;
        proc.Start();
        string result = proc.StandardOutput.ReadToEnd();
        Console.WriteLine(result);

1 个答案:

答案 0 :(得分:0)

向cmd.exe的参数添加/ k可以正常工作。

查看代码:

using System;

namespace ProcessRun
{
    class Program
    {
        static void Main(string[] args)
        {
            //var argumentString = "\"%VS120COMNTOOLS%/../IDE/devenv.exe\" /diff " + "\"" + txt_File1 + "\" \"" + txt_file2 + "\"";
            string argumentString = "start winword";

            // Method 1 
            Method1(argumentString);

            //Method 2
            Method2(argumentString);

            // Method 3
            Method3(argumentString);
        }

        private static void Method3(string argumentString)
        {
            System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", "/k " + argumentString);
            procStartInfo.RedirectStandardOutput = true;
            procStartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
            procStartInfo.UseShellExecute = false;
            procStartInfo.CreateNoWindow = false;
            System.Diagnostics.Process proc = new System.Diagnostics.Process();
            proc.StartInfo = procStartInfo;
            proc.Start();
            string result = proc.StandardOutput.ReadToEnd();
            Console.WriteLine(result);
        }

        private static void Method2(string argumentString)
        {
            System.Diagnostics.Process.Start(@"cmd.exe", "/k " + argumentString);
        }

        private static void Method1(string argumentString)
        {
            System.Diagnostics.Process ExecuteCommand = new System.Diagnostics.Process();
            System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
            startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
            startInfo.FileName = "cmd.exe";
            startInfo.UseShellExecute = true;

            startInfo.Arguments = "/k " + argumentString;
            ExecuteCommand.StartInfo = startInfo;
            ExecuteCommand.Start();
        }
    }
}