29

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"

3 Answers 3

39

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);
    }
Sign up to request clarification or add additional context in comments.

4 Comments

use /c instead /k if you want to get output :D
"using System.Diagnostics;" is required.
Tried /k and /c. It opens a cmd.exe window despite CreateNoWindow=true. The difference is only that with /k the window remains and with /c it opens and closes (after the command exits). I avoided opening of the window by setting UseShellExecute=false, but with all 4 combinations (/k or /c + UseShellExecute=true/false) I have no output in the parent console.
Another comment regarding usage of /k and UseShellExecute=false together: the cmd.exe window does not show up, but the cmd.exe remains hanging in the process list, so it gets necessary to kill it afterwards. I tested all 4 combinations on a simple command "echo 12345". To sum it up: this solution worked for me only with explicit output redirection into a file and the only combination free of side-effects like hanging cmd.exe or popup windows was UseShellExecute=false + /c.
28

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

1 Comment

You could redirect the standard output, wait for the process execution to finish and then read it.
24

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.