How to Execute a Command in C# ?

Command Line argument in c#













string source =@"""C:\Documents and Settings\manojb\Desktop\MSI.msi""";string target =@"""D:\New Folder""";string passive ="passive TARGETDIR";string MSI=@"""C:\Windows\System32\msiexec.exe""";string commandLine = MSI + "/" + passive + "=" + target + " /i " + source ;//Process pr1 = new Process();//// it enable to start and stop the local system process//pr1.StartInfo.FileName = "cmd.exe";//pr1.StartInfo.Arguments = commandLine;//pr1.StartInfo.RedirectStandardOutput = true;//pr1.StartInfo.UseShellExecute = false;//pr1.Start();//string output = pr1.StandardOutput.ReadToEnd();


//pr1.WaitForExit();//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",commandLine);// // 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 ex)//{// MessageBox.Show(ex.Message);

//}

System.Diagnostics.
StartInfo.FileName =
string tempGETCMD = null; Process CMDprocess = new Process(); ProcessStartInfo StartInfo = new System.Diagnostics.ProcessStartInfo(); "cmd"; //starts cmd window //StartInfo.WindowStyle = ProcessWindowStyle.Hidden; //StartInfo.CreateNoWindow = true; StartInfo.RedirectStandardInput =
StartInfo.RedirectStandardOutput =
StartInfo.UseShellExecute =
true; true; false; //required to redirect CMDprocess.StartInfo = StartInfo;
CMDprocess.Start();
System.IO.
System.IO.

SW.WriteLine(commandLine);
StreamReader SR = CMDprocess.StandardOutput; StreamWriter SW = CMDprocess.StandardInput; //insert your other commands here SW.WriteLine("exit"); //exits command prompt window tempGETCMD = SR.ReadToEnd(); //returns results of the command window SW.Close();
SR.Close();

Comments