Installing Windows Service programmatically
You can install the service by adding this code (in the program file, Program.cs) to install itself when run from the commandline using specified parameters:
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main(string[] args)
{
if (System.Environment.UserInteractive)
{
if (args.Length > 0)
{
switch (args[0])
{
case "-install":
{
ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location });
break;
}
case "-uninstall":
{
ManagedInstallerClass.InstallHelper(new string[] { "/u", Assembly.GetExecutingAssembly().Location });
break;
}
}
}
}
else
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[] { new MyService() };
ServiceBase.Run(ServicesToRun);
}
}
How to install a windows service programmatically in C#?
Ok, here is what REALLY worked for me, it has been tested on multiple machines with different OS ( Vista, XP, Win2k, Win2003 server )
The code has been taken from here so full credit goes to whoever wrote this piece of code.
Once you add the dll or source file into your project make sure to add the ServiceTools namespace and then you have access to some very handy functionality such as...
//Installs and starts the service
ServiceInstaller.InstallAndStart("MyServiceName", "MyServiceDisplayName", "C:\\PathToServiceFile.exe");
//Removes the service
ServiceInstaller.Uninstall("MyServiceName");
//Checks the status of the service
ServiceInstaller.GetServiceStatus("MyServiceName");
//Starts the service
ServiceInstaller.StartService("MyServiceName");
//Stops the service
ServiceInstaller.StopService("MyServiceName");
//Check if service is installed
ServiceInstaller.ServiceIsInstalled("MyServiceName");
I hope this helps.
Unable to install a Windows Service programmatically
public static class SelfInstaller
{
private static readonly string _exePath = Assembly.GetExecutingAssembly().Location;
public static bool InstallMyService()
{
try
{
ManagedInstallerClass.InstallHelper(new string[] { _exePath });
}
catch
{
return false;
}
return true;
}
public static bool UninstallMyService()
{
try
{
ManagedInstallerClass.InstallHelper(new string[] { "/u", _exePath });
}
catch
{
return false;
}
return true;
}
public static bool IsInstalled(string serviceName)
{
var serviceExists = ServiceController.GetServices().Any(s => s.ServiceName == serviceName);
if (serviceExists == null) return false;
return true;
}
}
Installing a windows service programmatically
It looks like the log source is null; are you sure that ServiceConfiguration.ServiceName
is defined and has a value?
Programmatically Installing Windows Service from Application
In general, ClickOnce can't be used to install services. There is typically a lack of permissions, but also the location is incorrect, etc. For details, see MSDN on Choosing Between ClickOnce and Windows Installer for more details.
If you want to install a service, you should do a traditional installation.
How to install Windows service to another system?
- Copy the executable and config files (in your case
FileMonitorService.exe
andFileMonitorService.exe.config
) on the target machine.
In case you need to run remote debugging please remember to also copyFileMonitorService.pdb
file. Install your service using
InstallUtil.exe
. You must use theInstallUtil.exe
of the framework version you used to develop your service. For example if your service has been developed with .net 4.x you must use theInstallUtil.exe
file in the following folder:C:\Windows\Microsoft.NET\Framework64\v4.0.30319
To install the service open a command prompt on the target machine, move to the location where you copied your files and launch the following command:
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe FileMonitorService.exe
More information on Windows service installation can be found here.
Related Topics
C# Constructing Parameter Query SQL - Like %
How to Detect That C# Windows Forms Code Is Executed Within Visual Studio
Identifying Active Network Interface
C# - Which Is the Best Alternative to 'Switch on Type'
How to Use Font Awesome Icons in Project as an Icon of Imagebutton
Microsoft Visual Studio and C#: How to Visually Add Events to Controls
JSONconvert.Deserializer Indexing Issues
How to Bind to a Dynamicresource So You Can Use a Converter or Stringformat, etc.? (Revision 4)
External Handler for Msiexec Msisetexternalui
How to Create Xmlelement Attributes with Prefix
C# Code for Association, Aggregation, Composition
How to Keep Single SQL Server Connection Instance Open for Multiple Request in C#
How to Merge Two Lists Using Linq