Run Command Prompt Commands

前端 未结 14 1913
暖寄归人
暖寄归人 2020-11-21 05:32

Is there any way to run command prompt commands from within a C# application? If so how would I do the following:

copy /b Image1.jpg + Archive.rar Image2.jp         


        
相关标签:
14条回答
  • 2020-11-21 06:25

    if you want to run the command in async mode - and print the results. you can you this class:

        public static class ExecuteCmd
    {
        /// <summary>
        /// Executes a shell command synchronously.
        /// </summary>
        /// <param name="command">string command</param>
        /// <returns>string, as output of the command.</returns>
        public static void ExecuteCommandSync(object command)
        {
            try
            {
                // create the ProcessStartInfo using "cmd" as the program to be run, and "/c " as the parameters.
                // Incidentally, /c tells cmd that we want it to execute the command that follows, and then exit.
                System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command);
                // The following commands are needed to redirect the standard output. 
                //This means that it will be redirected to the Process.StandardOutput StreamReader.
                procStartInfo.RedirectStandardOutput =  true;
                procStartInfo.UseShellExecute = false;
                // Do not create the black window.
                procStartInfo.CreateNoWindow = true;
                // Now we create a process, assign its ProcessStartInfo and start it
                System.Diagnostics.Process proc = new System.Diagnostics.Process();
                proc.StartInfo = procStartInfo;
                proc.Start();
    
                // Get the output into a string
                string result = proc.StandardOutput.ReadToEnd();
    
                // Display the command output.
                Console.WriteLine(result);
            }
            catch (Exception objException)
            {
                // Log the exception
                Console.WriteLine("ExecuteCommandSync failed" + objException.Message);
            }
        }
    
        /// <summary>
        /// Execute the command Asynchronously.
        /// </summary>
        /// <param name="command">string command.</param>
        public static void ExecuteCommandAsync(string command)
        {
            try
            {
                //Asynchronously start the Thread to process the Execute command request.
                Thread objThread = new Thread(new ParameterizedThreadStart(ExecuteCommandSync));
                //Make the thread as background thread.
                objThread.IsBackground = true;
                //Set the Priority of the thread.
                objThread.Priority = ThreadPriority.AboveNormal;
                //Start the thread.
                objThread.Start(command);
            }
            catch (ThreadStartException )
            {
                // Log the exception
            }
            catch (ThreadAbortException )
            {
                // Log the exception
            }
            catch (Exception )
            {
                // Log the exception
            }
        }
    
    }
    
    0 讨论(0)
  • 2020-11-21 06:26

    You can achieve this by using the following method (as mentioned in other answers):

    strCmdText = "'/C some command";
    Process.Start("CMD.exe", strCmdText);
    

    When I tried the methods listed above I found that my custom command did not work using the syntax of some of the answers above.

    I found out more complex commands need to be encapsulated in quotes to work:

    string strCmdText;
    strCmdText = "'/C cd " + path + " && composer update && composer install -o'";
    Process.Start("CMD.exe", strCmdText);
    
    0 讨论(0)
提交回复
热议问题