1

I want to run cmd command with C# for install service in Windows, I'm using of below code:

class Program
{
    static void Main(string[] args)
    {
        System.Diagnostics.Process process = new System.Diagnostics.Process();
        System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
        startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
        startInfo.CreateNoWindow = false;
        startInfo.FileName = "cmd.exe";
        startInfo.Arguments =
            "\"C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\installutil.exe\" \"D:\\Projects\\MyNewService\\bin\\Release\\MyNewService.exe\"";
        process.StartInfo = startInfo;
        process.Start();

    }
}

but this program don't work. if I run this command in cmd.exe work properly but when I run this project don't execute command and MyNewService.exe don't install.

where is my problem? do you help me?

2
  • 4
    Do you run cmd runas administrator? Commented Nov 2, 2015 at 11:11
  • 1
    yes, my project run as administrator. and before of this I added startInfo.Verb="runas"; but don't work. Commented Nov 2, 2015 at 11:15

1 Answer 1

3

Instead of starting a cmd.exe and passing installutil as an argument (then your service executabel an argument of the argument), try starting the installutil.exe executable directly passing the MyNewService.exe as the argument.

You should always wait for the process to exit, and should always check the exit code of the process as below.

static void Main(string[] args)
{

    System.Diagnostics.Process process = new System.Diagnostics.Process();
    System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
    startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
    startInfo.CreateNoWindow = true;
    startInfo.FileName = "C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\installutil.exe";
    startInfo.Arguments = "D:\\Projects\\MyNewService\\bin\\Release\\MyNewService.exe";
    process.StartInfo = startInfo;
    bool processStarted = process.Start();
    process.WaitForExit();
    int resultCode = process.ExitCode;
    if (resultCode != 0)
    {
        Console.WriteLine("The process intallutil.exe exited with code {0}", resultCode);
    }
}
Sign up to request clarification or add additional context in comments.

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.