MaviLe
MaviLe

Reputation: 15

How to install a Windows service (via installutil) in C#?

My code:

Process kurulum1 = new Process();
kurulum1.StartInfo.FileName = @"C:\ProgramData\Microsoft\Windows\StartMenu\Programs\Visual Studio 2017\Visual Studio Tools\Developer Command Prompt for VS 2017.lnk";

 kurulum1.StartInfo.Arguments = "cd C:\\Users\\stajyer3\\Documents\\Visual Studio 2017\\Projects\\TestWindowsService\\TestWindowsService\\bin\\Debug InstallUtil.exe "TestWindowService.exe"  ";

kurulum1.Start();

kurulum1.WaitForExit();

Not Working

Error:

[ERROR:parse_cmd.bat] Invalid command line argument: 'cd'. Argument will be ignored. [ERROR:parse_cmd.bat] Invalid command line argument: 'C:\Users\stajyer3\Documents\Visual'. Argument will be ignored. [ERROR:parse_cmd.bat] Invalid command line argument: 'Studio'. Argument will be ignored. [ERROR:parse_cmd.bat] Invalid command line argument: '2017\Projects\TestWindowsService\TestWindowsService\bin\Debug'. Argument will be ignored.


** Visual Studio 2017 Developer Command Prompt v15.0.26430.14 ** Copyright (c) 2017 Microsoft Corporation ********************************************************************** [ERROR:VsDevCmd.bat] * VsDevCmd.bat encountered errors. Environment may be incomplete and/or incorrect. *

Upvotes: 0

Views: 1961

Answers (3)

mjwills
mjwills

Reputation: 23974

Give this a whirl:

var kurulum1 = new Process
{
    StartInfo =
    {
        FileName = @"C:\Windows\Microsoft.NET\Framework\v4.0.30319\installutil.exe",
        WorkingDirectory =
            "C:\\Users\\stajyer3\\Documents\\Visual Studio 2017\\Projects\\TestWindowsService\\TestWindowsService\\bin\\Debug",
        Arguments = "TestWindowService.exe"
    }
};
kurulum1.Start();
kurulum1.WaitForExit();

There is no need to involve the Developer Command Prompt - you can just invoke installutil directly.

Upvotes: 0

MikeT
MikeT

Reputation: 5500

you have made many mistakes here , with out knowing your objective its very difficult to say which is the worst

  1. ProcessStartInfo.FileName should be the file you want to execute not a start menu shortcut

  2. ProcessStartInfo.Arguments is a command line argument not a command

  3. the command you are using seems to be an attempt to change the working directory this should be done with ProcessStartInfo.WorkingDirectory

  4. the strings you are using are broken because you are exiting the strings by using " but then you carry the string on, so i must assume you mean the " to be inside the string in which case you need to delimit them with \ this would then look like "he said \"delimit your strings\" "though if you are using the @ notation then the delimiter becomes "" not \"

  5. arguments are space separated strings so if your arguments contain spaces such as long form filenames then you need to surround them with quotes " so ProcessStartInfo.Arguments = "a b c:\\temp"; will pass the args "a", "b" and "c:\temp" to the executing program but ProcessStartInfo.Arguments = "a literal string arg"; would pass each word as as separate argument ProcessStartInfo.Arguments = "\"a literal string arg\""; and this will pass in a single arg with the entire string

Upvotes: 3

David Lindon
David Lindon

Reputation: 355

You need to enclose the path in quotes (and escape the quotes around the filename)

kurulum1.StartInfo.Arguments = "cd \"C:\\Users\\stajyer3\\Documents\\Visual Studio 2017\\Projects\\TestWindowsService\\TestWindowsService\\bin\\Debug InstallUtil.exe\" \"TestWindowService.exe\"  ";

Upvotes: 0

Related Questions