68

Just tried to run an application via the following:

enter image description here

I have browsed to the directory with an app WindowsService1.exe in it, then tried the command Installutil WindowsService1.exe but got the following error...

enter image description here

As VS has only been installed for a day or two I'm worried that something may be wrong with that install as it should recognise installutil.

Are there some basic diagnostics I can perform to ensure that VS Command Prompt is finding all the programs that it should ?

EDIT

If i run PATH in the command prompt I see the following:

enter image description here

user3188978
  • 119
  • 2
  • 15
whytheq
  • 34,466
  • 65
  • 172
  • 267
  • 2
    did you try it running as an administrator? – Agent007 Oct 03 '12 at 07:23
  • You can try to do a repair from the "Add or Remove Programs" window. – Erwin Oct 03 '12 at 07:26
  • If you type `PATH` from the VS Command Prompt you should see a sizable list of path variables (on my machine, I see about 20 lines worth). If this list is small then there may be something wrong with the install. If installutil is on the machine but not being located, it's a path variable problem. If it's missing altogether, that's a different problem. – Tim M. Oct 03 '12 at 07:26
  • if I open command prompt as administrator how do I install a file that is on the `R:\Drive` i.e if I right click in the Start menu and choose "Run as Administrator" how do I get out of the `C:\Drive`? If I type the command `R:` it doesn't go to the R-drive which is the location of the file I wish to install – whytheq Oct 03 '12 at 07:27
  • `installutil "r:\path here\"` (quotes important if there are spaces in path) – Tim M. Oct 03 '12 at 07:29
  • @TimMedora ...let me add a screenshot to the OP of what I see when commanding `PATH`.. – whytheq Oct 03 '12 at 07:29
  • Your path appears to be missing `C:\Windows\Microsoft.NET\Framework\version`. Not sure what would cause that, but that's where installutil is located and why it's not being found. – Tim M. Oct 03 '12 at 07:34
  • @TimMedora Yep - should have `C:\Windows\Microsoft.NET\Framework\v4.0.30319` ..What do you suggest? - uninstall VS and re-install it? I wonder if this has anything to do with me installing VB.NET Express before installing VS 2012 ? – whytheq Oct 03 '12 at 07:40
  • You can find the actual path settings in `C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\bin\vcvars32.bat` (on x86)...but I wouldn't suggest modifying that unless you really know what you are doing. You could try repairing the .Net framework first before reinstalling. Beyond that, I don't know what to suggest. – Tim M. Oct 03 '12 at 07:43

13 Answers13

142

This is a tiny bit off-topic but I've stopped using InstallUtil to install my services. It's is really easy to just add it to the service itself. Add a reference to System.Configuration.Install (not available in the Client Profile editions if I remember right) and then update your Main()-function in Program.cs like this.

static void Main(string[] args) {
    if (Environment.UserInteractive) {
        var parameter = string.Concat(args);
        switch (parameter) {
            case "--install":
                ManagedInstallerClass.InstallHelper(new[] { Assembly.GetExecutingAssembly().Location });
                break;
            case "--uninstall":
                ManagedInstallerClass.InstallHelper(new[] { "/u", Assembly.GetExecutingAssembly().Location });
                break;
        }
    } else {
        ServiceBase[] servicesToRun = { 
            new Service1() 
        };
        ServiceBase.Run(servicesToRun);
    }
}

Then you can just call WindowsService1.exe with the --install argument and it will install the service and you can forget about InstallUtil.exe.

Karl-Johan Sjögren
  • 16,544
  • 7
  • 59
  • 68
  • 10
    Catch InvalidOperationException for installation failures and InstallException for uninstallation failures [security and already (un)installed]. – jdknight May 09 '13 at 20:36
  • So much better, although I don't get the user interactive part. This is always true when i'm debugging so my service never starts. – KingOfHypocrites Dec 13 '13 at 02:57
  • 9
    `Environment.UserInteractive` is true when starting the executable directly, ie not via the servicemanager but through the commandline or Visual Studio. You could add a check of `System.Diagnostics.Debugger.IsAttached` as well to skip that part when debugging in Visual Studio. – Karl-Johan Sjögren Dec 13 '13 at 05:37
  • 1
    FYI, I've tried this with a service built in VS 2013, and it seems successful based on the output when doing --install, but the service is nowhere to be seen in the Services control panel. – Mason G. Zhwiti Mar 18 '14 at 16:31
  • I've used this in a service built with Visual Studio 2013 and .Net 4.5 and it registered just fine. Does your service register if you use installutil.exe instead? – Karl-Johan Sjögren Mar 18 '14 at 17:51
  • 2
    @Karl-JohanSjögren We just uninstalled, and then used installutil and had the same issue... so it was not related to this technique. It turns out the developer that setup our installer left out a lot of the necessary code in the installer constructor that is shown on the example on this page: http://msdn.microsoft.com/en-us/library/system.serviceprocess.serviceinstaller.aspx – Mason G. Zhwiti Mar 18 '14 at 20:02
  • today i noticed that 32 bit applications need another installutil than 64 applications. then i found your solution. thanks a lot! (seems to work fine in VS2013, .net 4.5) – stmax Oct 13 '14 at 12:39
  • what is `ValidatorService`? – Marco Dinatsoli Oct 22 '14 at 13:31
  • Thats the name of my service class. This would be `Service1` if you just created a new service project and didn't touch the generated name. I'll update the example so it says `Service1` instead since thats what most will be seeing. – Karl-Johan Sjögren Oct 22 '14 at 13:52
  • Love this approach however, using `Environment.UserInteractive` just burned me for 2.5 hours. We were attempting to install our service via a Powershell script executed from another service (Visual Studio Online Agent). Seems logical but it took abit to figure out the issue. We are going to be more explicit and use a `--console` approach to launch the console rather than using `Environment.UserInteractive`. – Rick Glos Oct 26 '15 at 21:03
  • Good catch! The whole `UserInteractive` part is just so that you doesn't start the service from commandline if you misspell something, it could easily be skipped and `return;` used instead of `break;`. – Karl-Johan Sjögren Oct 27 '15 at 12:46
  • I received 'Error 1053 the service did not respond to the start or control request in a timely fashion', when starting service from 'services.msc'. – Hadi Akbarzadeh Feb 20 '20 at 12:07
  • Hi! Any idea how to make this service start automatically? I set StartType to Automatic. So it is "Automatic" in services.msc.. But it doesn't unless I manually start it – Macindows Jun 15 '20 at 09:05
  • Services set to "Automatic" will start up when Windows starts. If you want to start it directly after registering it you'll need to do it yourself using [ServiceConroller](https://learn.microsoft.com/en-us/dotnet/api/system.serviceprocess.servicecontroller?view=dotnet-plat-ext-3.1) – Karl-Johan Sjögren Jun 15 '20 at 12:54
  • Question Console in admin to install with this method? Can it be used via Project propertiy> debugging > start up parameter? – Drag and Drop Jan 13 '21 at 11:00
  • Installing requires elevation, but if you start the application without the --install parameter it will run the service as usual. – Karl-Johan Sjögren Jan 13 '21 at 11:28
  • As running as administrator was the issue in the original question. not mentionning it in this great answers feelled like it was not a necessity. – Drag and Drop Jan 13 '21 at 13:42
  • Well no, the original problem was `installutil.exe` not being available in `%PATH%` which is what this is the primary workaround for. – Karl-Johan Sjögren Jan 13 '21 at 15:08
  • getting "The name Assembly does not exist in the current context" how to fix that ?! – asmgx Feb 02 '21 at 00:48
  • 2
    You need to add `using System.Reflection;`. – Karl-Johan Sjögren Feb 02 '21 at 08:15
48

This is what I have done to make it go away:

  1. Found where installutil resides on my PC. In my case it was C:\Windows\Microsoft.NET\Framework\v4.0.30319

  2. Opened a command prompt as an Administrator and changed current directory to above: 'cd C:\Windows\Microsoft.NET\Framework\v4.0.30319'

  3. Then entered: 'installutil C:\MyProgramName.exe'

Interestingly, prior to above solution I tried different options, among them adding C:\Windows\Microsoft.NET\Framework\v4.0.30319 to the System Path variable, but it still could not find it.

Wish you all smooth installation.

user3085805
  • 581
  • 5
  • 3
28

InstallUtil.exe is typically found under one of the versions listed under C:\Windows\Microsoft.NET\Framework.

In my case it is under v4.0.30319.

You could just check your path:

echo %PATH%

should give you a list of directories searched for executables.

Daniel
  • 891
  • 6
  • 18
  • VisualStudio command prompt has an installutil command. http://msdn.microsoft.com/en-us/library/sd8zc8ha.aspx – margabit Oct 03 '12 at 07:27
  • 5
    The command prompt in VS is basically a cmd with a different PATH. – Daniel Oct 03 '12 at 07:29
  • yep - I have `InstallUtil.exe` in the same location as you Daniel i.e `C:\Windows\Microsoft.NET\Framework\v4.0.30319` and as you can see from the screenshot I've added to the OP this is not a default pathway of command prompt ...what do you suggest to fix this? – whytheq Oct 03 '12 at 07:43
  • Did you install a Windows SDK after the VS install? I remember seeing problems with the PATH getting broken after such an install. Look for vcvars32.bat in your bin-folder (i.e. under VC/bin folder of your install location). – Daniel Oct 03 '12 at 08:06
  • Thanks Daniel I forgot the method. – TAHA SULTAN TEMURI May 10 '17 at 20:00
5

Found a solution on bytes.com

The code to install a service:

@ECHO Installing Service...
@SET PATH=%PATH%;C:\Windows\Microsoft.NET\Framework\v4.0.30319\
@InstallUtil  C:\Unlock_4_Service\bin\Debug\Unlock_4_Service.exe
@ECHO Install Done.
@pause

@InstallUtil <.exe file path of your windows service>

Code to uninstall the service

@ECHO Installing Service...
@SET PATH=%PATH%;C:\Windows\Microsoft.NET\Framework\v4.0.30319\
@InstallUtil /u C:\Unlock_4_Service\bin\Debug\Unlock_4_Service.exe
@ECHO Uninstall Done.
@pause

@InstallUtil /u <.exe file path of your windows service >

Save the 2 files as service_install.bat and service_uninstall.bat

Run the files as administrator, every time you have to install or uninstall the service. enter image description here

user3188978
  • 119
  • 2
  • 15
5

Just add the installUtil.exe path in the environment variable to fix this issue.

Example:

 C:\Windows\Microsoft.NET\Framework\v4.0.30319
T. Short
  • 3,481
  • 14
  • 30
Yogi
  • 51
  • 1
  • 1
4

Before Installing service using command line...

use 2 steps:

  1. cd C:\Windows\Microsoft.NET\Framework\v4.0.30319
  2. InstallUtil.exe Path\MyWindowsService.exe
2

Unless you've modified your path, the following should be available in developer command prompt and not cmd:

  • msbuild
  • mstest(for ultimate)
  • csc
  • ilasm

... etc

If those aren't available you may have a corrupted install.

David Mason
  • 1,545
  • 2
  • 14
  • 14
2

This might have occurred because you would not have opened the Command Prompt as an administrator or with Administrative Privileges.

jasonscript
  • 6,039
  • 3
  • 28
  • 43
Ammu
  • 21
  • 1
2

open visual studio command prompt in admin mode i.e., right click on vs command prompt and run as administrator

2

According Microsoft Page :

If you’re using the Visual Studio command prompt, InstallUtil.exe should be on the system path. If not, you can add it to the path, or use the fully qualified path to invoke it. This tool is installed with the .NET Framework, and its path is :

%WINDIR%\Microsoft.NET\Framework[64]\

For example, for the 32-bit version of the .NET Framework 4 or 4.5.*, if your Windows installation directory is C:\Windows, the path is :

C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe

For the 64-bit version of the .NET Framework 4 or 4.5.*, the default path is :

C:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe

  • and after this, you need to start the service using the command: `net start [Serive Name]` from the command prompt with administrative Privilege. – Jayesh Baviskar Mar 05 '19 at 07:39
1

I got this after I had went back to 2015 from 2017 and I was still using the 2017 command prompt. Something to check.

Todd Vance
  • 4,627
  • 7
  • 46
  • 66
1
Add this in windows Environmental variables
First: Right click on My computer or This PC
Second: Click on Environmental Variables
Third: add this path after clicking on path
C:\Windows\Microsoft.NET\Framework\v4.0.30319\installutil.exe
ranojan
  • 819
  • 8
  • 11
1

This is somewhat off-topic as it's for use in a C# program, not for command-line use. What we did was locate the InteropServices runtime directory at runtime, eliminating the need to add or use a Path variable. This directly gets the Microsoft.NET Framework utilities folder. Here's a method.

using System.Diagnostics;

    public static void InstallService(string serviceExe, bool uninstall = false)
    {
        string installUtilPath = System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory() + "InstallUtil.exe";
        Process installProc = new Process();
        installProc.StartInfo.FileName = installUtilPath;
        installProc.StartInfo.Arguments = (uninstall ? " /u " : string.Empty) + @"""" + serviceExe + @"""";
        installProc.StartInfo.UseShellExecute = false;
        installProc.StartInfo.RedirectStandardOutput = true;
        installProc.StartInfo.RedirectStandardError = true;
        installProc.StartInfo.CreateNoWindow = true;
        installProc.Start();
        installProc.WaitForExit();
        Debug.WriteLine(installProc.StandardOutput.ReadToEnd());
        if (installProc.ExitCode != 0)
        {
            throw new Exception($"Failed to install service: {serviceExe}. Exit code: {installProc.ExitCode}\n Review {serviceExe}.InstallLog for details.\n" +
                $" Utility path: {installUtilPath}.");
        }
    }
pwrgreg007
  • 313
  • 3
  • 13