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 get the currently-logged username from a Windows service in .NET?

This is a WMI query to get the user name: ManagementObjectSearcher searcher = new ManagementObjectSearcher(“SELECT UserName FROM Win32_ComputerSystem”); ManagementObjectCollection collection = searcher.Get(); string username = (string)collection.Cast<ManagementBaseObject>().First()[“UserName”]; You will need to add System.Management under References manually.

Start / Stop a Windows Service from a non-Administrator user account

Below I have put together everything I learned about Starting/Stopping a Windows Service from a non-Admin user account, if anyone needs to know. Primarily, there are two ways in which to Start / Stop a Windows Service. 1. Directly accessing the service through logon Windows user account. 2. Accessing the service through IIS using Network … 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 can I verify if a Windows Service is running

I guess something like this would work: Add System.ServiceProcess to your project references (It’s on the .NET tab). using System.ServiceProcess; ServiceController sc = new ServiceController(SERVICENAME); switch (sc.Status) { case ServiceControllerStatus.Running: return “Running”; case ServiceControllerStatus.Stopped: return “Stopped”; case ServiceControllerStatus.Paused: return “Paused”; case ServiceControllerStatus.StopPending: return “Stopping”; case ServiceControllerStatus.StartPending: return “Starting”; default: return “Status Changing”; } Edit: There … Read more

.NET console application as Windows service

I usually use the following techinque to run the same app as a console application or as a service: using System.ServiceProcess public static class Program { #region Nested classes to support running as service public const string ServiceName = “MyService”; public class Service : ServiceBase { public Service() { ServiceName = Program.ServiceName; } protected override … Read more

How to start a process from windows service into currently logged in user’s session

The problem with Shrike’s answer is that it does not work with a user connected over RDP. Here is my solution, which properly determines the current user’s session before creating the process. It has been tested to work on XP and 7. https://github.com/murrayju/CreateProcessAsUser Everything you need is wrapped up into a single .NET class with … Read more