0Day Forums
Run command line code programmatically using C# - Printable Version

+- 0Day Forums (https://0day.red)
+-- Forum: Coding (https://0day.red/Forum-Coding)
+--- Forum: Asp.Net (https://0day.red/Forum-Asp-Net)
+--- Thread: Run command line code programmatically using C# (/Thread-Run-command-line-code-programmatically-using-C)



Run command line code programmatically using C# - increaseful651635 - 07-23-2023

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"


RE: Run command line code programmatically using C# - whemmel169955 - 07-23-2023

You may use the [`Process.Start`][1] 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`][2] 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);


[1]:

[To see links please register here]

[2]:

[To see links please register here]




RE: Run command line code programmatically using C# - horehounds663158 - 07-23-2023

You should be able to do that using a process

var proc = new Process();
proc.StartInfo.FileName = @"C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe ";
proc.StartInfo.Arguments = string.Format(@"{0} ""{1}""" ""{2}""","-pdf","connection Strings" ,"C:\Users\XXX\Desktop\connection string\DNN");
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.Start();
string outPut = proc.StandardOutput.ReadToEnd();

proc.WaitForExit();
var exitCode = proc.ExitCode;
proc.Close();


RE: Run command line code programmatically using C# - bluecoat752 - 07-23-2023

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);
}