- Start or stop Windows service from command line (CMD)
- How to Start or Stop a Service in Windows 10/8/7
- How to start, stop, or disable a service in Services.MSC
- How to Start, Stop or Restart Services in Task Manager
- How to Start or Disable a Service in Registry Editor
- How can I run an EXE program from a Windows Service using C#?
- 9 Answers 9
Start or stop Windows service from command line (CMD)
We normally use Services.msc to start or stop or disable or enable any service. We can do the same from windows command line also using net and sc utilities. Below are commands for controlling the operation of a service.
Command to stop a service:
To start a service:
You need to have administrator privileges to run net start/stop commands. If you are just a normal user on the computer, you would get an error like below.
To disable a service:
To enable a service:
To make a service start automatically with system boot:
Note: Space is mandatory after ‘=’ in the above sc commands.
This SC command works on a Windows 7 machine and also on the down-level editions of Windows i.e Windows XP/2003 and Windows Vista. Again, if you do not have administrator previliges you would get the below error.
Note that the service name is not the display name of a service. Each service is given a unique identification name which can be used with net or sc commands. For example, Remote procedure call (RPC) is the display name of the service. But the service name we need to use in the above commands is RpcSs.
So to start Remote procedure call service the command is:
These service names are listed below for each service. The first column shows the display name of a service and the second column shows the service name that should be used in net start or net stop or sc config commands.
How to Start or Stop a Service in Windows 10/8/7
A service is an application type that runs in the system background without any user interface, and without needing a user to log in to the PC. You can manage them start, stop, and disable actions for services on local and remote computers.
How to start, stop, or disable a service in Services.MSC
This is the easiest way to start or disable a Windows Service.
Step 1: Open the Services snap-in window.
Press the Win + R keys to bring up Run dialog box, then type in services.msc, press Enter key.
Step 2: Then you Start, Stop, or Disable any service you want to change its action. To start a service, right-click on it, and then click Start.
Be careful when choosing to stop or restart services, because if you set these services to Disabled, Windows will prevent them from launching.
Step 3: You can manage a program Startup type by double-clicking on it.
In General tab, you can set a service to run as either automatically, manually at startup when a user signs in to Windows, or disabled a program to speed up boots process.
Here are the different startup types you’ll see for various services:
How to Start, Stop or Restart Services in Task Manager
You can easily start, stop, and restart a service in Task Manager.
Press Ctrl+Alt+Delete combination keys, then select Task Manager in security option screen.
Step 2: If it opens in mini mode, click onВ More detailsВ button.
Step 3: Click the Service tab. This tab displays all programs that start with Windows.
Then you can start a Service by right-clicking on it, then select Start.
Step 4: To disable or delay loading of Windows Services.
You can choose to stop or restart the Service which is in stopped Status.
How to Start or Disable a Service in Registry Editor
You can start, stop or disable a service by modifying registry key.
Press theВ Windows + RВ keys to open the Run dialog, type inВ regedit, and press Enter.
Tips: If it prompts a UAC dialog, click Yes. Learn more ways to disable UAC pop-up in Window10.
Step 2: In Registry Editor, go to the location of that service (example: VMTools):
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\VMTools
In the right pane of VMTools, double-click on Start В DWORD to modify its DWORD.
You can set that service to start automatically (Value data = 2), manually, or disable it. If I want to start that service manually, so I type digital 3 in that box under Value data.
Tips: There are a few Automatic services you could consider setting to Manual:
- If you don’t use a printer,В disable the Print Spooler service.
- If you’re notВ running a Tablet PC, disable the Tablet PC Input service.
- If you don’t connect cameras, webcams, or scanners to your PC, then disable the Windows Image Acquisition service.
When you are finished, close Registry Editor, and then restart computer.
How can I run an EXE program from a Windows Service using C#?
How can I run an EXE program from a Windows Service using C#?
This is my code:
When I run this service, the application is not starting.
What’s wrong with my code?
9 Answers 9
This will never work, at least not under Windows Vista or later. The key problem is that you’re trying to execute this from within a Windows Service, rather than a standard Windows application. The code you’ve shown will work perfectly in a Windows Forms, WPF, or Console application, but it won’t work at all in a Windows Service.
Windows Services cannot start additional applications because they are not running in the context of any particular user. Unlike regular Windows applications, services are now run in an isolated session and are prohibited from interacting with a user or the desktop. This leaves no place for the application to be run.
More information is available in the answers to these related questions:
The best solution to your problem, as you’ve probably figured out by now, is to create a standard Windows application instead of a service. These are designed to be run by a particular user and are associated with that user’s desktop. This way, you can run additional applications whenever you want, using the code that you’ve already shown.
Another possible solution, assuming that your Console application does not require an interface or output of any sort, is to instruct the process not to create a window. This will prevent Windows from blocking the creation of your process, because it will no longer request that a Console window be created. You can find the relevant code in this answer to a related question.
i have tried this article Code Project, it is working fine for me. I have used the code too. article is excellent in explanation with screenshot.
I am adding necessary explanation to this scenario
You have just booted up your computer and are about to log on. When you log on, the system assigns you a unique Session ID. In Windows Vista, the first User to log on to the computer is assigned a Session ID of 1 by the OS. The next User to log on will be assigned a Session ID of 2. And so on and so forth. You can view the Session ID assigned to each logged on User from the Users tab in Task Manager.
But your windows service is brought under session ID of 0. This session is isolated from other sessions. This ultimately prevent the windows service to invoke the application running under user session’s like 1 or 2.
In order to invoke the application from windows service you need to copy the control from winlogon.exe which acts as present logged user as shown in below screenshot.
you can use from windows task scheduler for this purpose, there are many libraries like TaskScheduler that help you.
for example consider we want to scheduling a task that will executes once five seconds later:
notepad.exe will be executed five seconds later.
for details and more information please go to wiki
if you know which class and method in that assembly you need, you can invoke it yourself like this:
Top answer with most upvotes isn’t wrong but still the opposite of what I would post. I say it will totally work to start an exe file and you can do this in the context of any user. Logically you just can’t have any user interface or ask for user input.
Here is my advice:
- Create a simple Console Application that does what your service should do right on start without user interaction. I really recommend not using the Windows Service project type especially because you (currently) can’t using .NET Core.
- Add code to start your exe you want to call from service
Example to start e.g. plink.exe. You could even listen to the output:
- Use NSSM (Non-Sucking Service Manager) to register that Console Application as service. NSSM can be controlled via command line and can show an UI to configure the service or you configure it via command line. You can run the service in the context of any user if you know the login data of that user.
I took LocalSystem account which is default and more than Local Service. It worked fine without having to enter login information of a specific user. I didn’t even tick the checkbox «Allow service to interact with desktop» which you could if you need higher permissions.
Lastly I just want to say how funny it is that the top answer says quite the opposite of my answer and still both of us are right it’s just how you interpret the question :-D. If you now say but you can’t with the windows service project type — You CAN but I had this before and installation was sketchy and it was maybe kind of an unintentional hack until I found NSSM.
You can execute an .exe from a Windows service very well in Windows XP. I have done it myself in the past.
You need to make sure you had checked the option «Allow to interact with the Desktop» in the Windows service properties. If that is not done, it will not execute.
I need to check in Windows 7 or Vista as these versions requires additional security privileges so it may throw an error, but I am quite sure it can be achieved either directly or indirectly. For XP I am certain as I had done it myself.
First, we are going to create a Windows Service that runs under the System account. This service will be responsible for spawning an interactive process within the currently active User’s Session. This newly created process will display a UI and run with full admin rights. When the first User logs on to the computer, this service will be started and will be running in Session0; however the process that this service spawns will be running on the desktop of the currently logged on User. We will refer to this service as the LoaderService.
Next, the winlogon.exe process is responsible for managing User login and logout procedures. We know that every User who logs on to the computer will have a unique Session ID and a corresponding winlogon.exe process associated with their Session. Now, we mentioned above, the LoaderService runs under the System account. We also confirmed that each winlogon.exe process on the computer runs under the System account. Because the System account is the owner of both the LoaderService and the winlogon.exe processes, our LoaderService can copy the access token (and Session ID) of the winlogon.exe process and then call the Win32 API function CreateProcessAsUser to launch a process into the currently active Session of the logged on User. Since the Session ID located within the access token of the copied winlogon.exe process is greater than 0, we can launch an interactive process using that token.
I think You are copying the .exe to different location. This might be the problem I guess. When you copy the exe, you are not copying its dependencies.
So, what you can do is, put all dependent dlls in GAC so that any .net exe can access it
Else, do not copy the exe to new location. Just create a environment variable and call the exe in your c#. Since the path is defined in environment variables, the exe is can be accessed by your c# program.
previously I had some kind of same issue in my c#.net 3.5 project in which I was trying to run a .exe file from c#.net code and that exe was nothing but the another project exe(where i added few supporting dlls for my functionality) and those dlls methods I was using in my exe application. At last I resolved this by creating that application as a separate project to the same solution and i added that project output to my deployment project. According to this scenario I answered, If its not what he wants then I am extremely sorry.