Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run command line code programmatically using C#

Tags:

I'm using this code run in windows command prompt.. But I need this done programmatically using C# code

C:\Windows\Microsoft.NET\Framework\v4.0.30319>aspnet_regiis.exe -pdf "connection Strings" "C:\Users\XXX\Desktop\connection string\DNN"

like image 305
CNB Avatar asked Dec 06 '12 06:12

CNB


2 Answers

try this

ExecuteCommand("Your command here"); 

call it using process

 public void ExecuteCommand(string Command)     {         ProcessStartInfo ProcessInfo;         Process Process;          ProcessInfo = new ProcessStartInfo("cmd.exe", "/K " + Command);         ProcessInfo.CreateNoWindow = true;         ProcessInfo.UseShellExecute = true;          Process = Process.Start(ProcessInfo);     } 
like image 95
Microsoft DN Avatar answered Sep 27 '22 22:09

Microsoft DN


You may use the Process.Start method:

Process.Start(     @"C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe",     @"-pdf ""connection Strings"" ""C:\Users\XXX\Desktop\connection string\DNN""" ); 

or if you want more control over the shell and be able to capture for example the standard output and error you could use the overload taking a ProcessStartInfo:

var psi = new ProcessStartInfo(@"C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe") {     Arguments = @"-pdf ""connection Strings"" ""C:\Users\XXX\Desktop\connection string\DNN""",     UseShellExecute = false,     CreateNoWindow = true }; Process.Start(psi); 
like image 24
Darin Dimitrov Avatar answered Sep 27 '22 23:09

Darin Dimitrov