How to install a windows service programmatically in C#?

I found several errors in the code that you reused and have fixed these and also cleaned it up a little. Again, the original code is taken from here. public static class ServiceInstaller { private const int STANDARD_RIGHTS_REQUIRED = 0xF0000; private const int SERVICE_WIN32_OWN_PROCESS = 0x00000010; [StructLayout(LayoutKind.Sequential)] private class SERVICE_STATUS { public int dwServiceType = … Read more

How do I debug Windows services in Visual Studio?

Use the following code in service OnStart method: System.Diagnostics.Debugger.Launch(); Choose the Visual Studio option from the pop up message. Note: To use it in only Debug mode, a #if DEBUG compiler directive can be used, as follows. This will prevent accidental or debugging in release mode on a production server. #if DEBUG System.Diagnostics.Debugger.Launch(); #endif

System.Security.SecurityException: The source was not found, but some or all event logs could not be searched. Inaccessible logs: Security

I got the same exception when trying to install a service from the command line when using installutil in Windows 7. The solution was to open the command line as Administrator and then run installutil. Also you may find it easier to use a framework like TopShelf to host your services, because it manages all … Read more

How might I schedule a C# Windows Service to perform a task daily?

I wouldn’t use Thread.Sleep(). Either use a scheduled task (as others have mentioned), or set up a timer inside your service, which fires periodically (every 10 minutes for example) and check if the date changed since the last run: private Timer _timer; private DateTime _lastRun = DateTime.Now.AddDays(-1); protected override void OnStart(string[] args) { _timer = … Read more

How to make a .NET Windows Service start right after the installation?

I’ve posted a step-by-step procedure for creating a Windows service in C# here. It sounds like you’re at least to this point, and now you’re wondering how to start the service once it is installed. Setting the StartType property to Automatic will cause the service to start automatically after rebooting your system, but it will … Read more

Install Windows Service with Recovery action to Restart

You can set the recovery options using sc. The following will set the service to restart after a failure: sc failure [servicename] reset= 0 actions= restart/60000 This can easily be called from C#: static void SetRecoveryOptions(string serviceName) { int exitCode; using (var process = new Process()) { var startInfo = process.StartInfo; startInfo.FileName = “sc”; startInfo.WindowStyle … Read more

What directory does a Windows Service run in?

System.Diagnostics.Trace.WriteLine(Directory.GetCurrentDirectory()); will output the current directory. Put that code in the startup method of your service and use a tool like DebugView to check the output. Then you will know the startup folder of your service. This simple technique will be useful with many problems in service development, especially to debug service startup. You probably … Read more

How do I create an InstallShield LE project to install a windows service?

For Visual Studio 2012 & InstallShield LE, do the following: Run through the InstallShield project assistant and add the primary output of your service to the Application files section. After you are done with the project assistant, double click the “Files” item under step two of the setup project. Right click on the primary output … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)