SoFunction
Updated on 2025-03-07

C# How to execute CMD commands and receive the returned results

Recently, when I was working, I found that when I searched the ARP entry corresponding to a certain IP in the software by querying the ARP table, the ARP entry found probability appeared to be empty. At first, I suspected that I could not learn ARP.

Later I thought that this was impossible. Finally, after various analysis, I found that the operation of calling ARP in the software was implemented through WinExec in the call. This function will return as long as the call is successful, and will not wait for the called program to be executed before returning.

Therefore, on some computers that are slow to respond, it will appear: if your operation order is to clear ARP, ping, and query ARP, it may appear that the ARP table will be cleared after the ping is completed, resulting in the ARP entry not being found.

The implementation method of querying C# to call the program online and waiting for the program to be executed before returning is as follows:

1.Introduction

using ;  

2. Create and execute CMD

Process CmdProcess = new Process();  
 = "";    

3. Input and output errors in configuration development method

 = true;         // No new window is created = false;       //Do not enable shell startup process = true;  // Redirect input = true; // Redirecting standard output = true;  // Redirect error output  

4. Execute cmd and get the return value

Method 1

 = "/c " + "=====cmdOrder======";//"/C" means to exit immediately after executing the command();//implement();//Get the return value();//Waiting for the program to be executed and exiting the process();//Finish 

Method 2

(str + "&exit"); //Send input information to cmd window = true;  //submit();//implement();//Output();//Waiting for the program to be executed and exiting the process();//Finish  

5. Output return value

First introduce

using ;
 
            StreamReader sr =;//Get the return value            string line = ""; 
            int num = 1;
            while ((line=())!=null)
            {   
                if(line!="")
                {
                    (line + " " + num++);
                }
            }

HasExited property

    //Waiting for the program to be executed and exiting the process    ();
 
    //Judge the program is exiting the process and exit is true (after the above exit method is executed, the return value of HasExited is true)     falg = ; 

Supplement: C# dynamically calls the executable program and accepts the return value

Look at the code ~

        static void Main(string[] args)
        {
            object output;
            try
            {
                using (Process p = new Process())
                {
                     = @"";//Executable program path                     = "";//The parameters are separated by spaces. If a parameter is empty, you can pass in ""                     = false;//Whether to start using the operating system shell                     = true;//The program window is not displayed                     = true;//Get the output information by the caller                     = true;   //Accept input information from the calling program                     = true;   //Redirection standard error output                    ();
                    ();
                    //After normal operation, put back the code to 0                    if ( != 0)
                    {
                        output = ();
                        output = ().Replace(, );
                        output = ().Replace("\n", );
                        throw new Exception(());
                    }
                    else
                    {
                        output = ();
                    }
                }
                (output);
            }
            catch (Exception ee)
            {
                ();
            }
            ();
        }

The above is personal experience. I hope you can give you a reference and I hope you can support me more. If there are any mistakes or no complete considerations, I would like to give you advice.