We write our Windows service
Many of us are faced with this problem when we need to run our application when the computer starts. Of course, you can put a shortcut in startup, but somehow it's wrong. And besides, if the computer rebooted, and the user did not log in, then your application will not start either.
The surest solution in this situation is to write a Windows service.
An example of creating a service in Studio 2010, .Net C # under cat
Create a new project by selecting the Windows Service template

Rename the class of service as you need.

Received such a water code:
This is, in fact, the service itself.
Use OnStart and OnStop events to implement your task.
To make your service work, you need to install it.
To install it, it must have an installer.
Right click ... Add installer

Now we have serviceProcessInstaller and serviceInstaller.

In the first you can set the Account value to LocalSystem .
In the second, specify the service name, description and do not forget to put StartType - Automatic .

The installer is ready.
In order for you to be able to find out what your service was doing, when it started, ended or something else, you can use system logging.
This is done very easily.
Drag from Toolbox to your EventLog service .

This is how logging is done:
To install the service, you need to call the installation utility and pass the path to your service as a parameter.
To do this, I created install.bat of this form: If you selected the Account - User field value in serviceProcessInstaller , then during installation you will have to enter the login and password of the account under which the service will be launched. Attention! Writing a domain before the username is required! We launch the batch file with administrator rights and observe at the end: The Commit phase completed successfully. The transacted install has completed. This means that the service is installed. Check: Installed. We start and stop a couple of times. We look at the logs:


We see when the service started and stopped.
Our service is ready.
The material is partially taken from here:
msdn.microsoft.com/en-us/library/zt39148a (VS.80) .aspx
The surest solution in this situation is to write a Windows service.
An example of creating a service in Studio 2010, .Net C # under cat
Step 1. Create a project.
Create a new project by selecting the Windows Service template

Rename the class of service as you need.

Received such a water code:
namespace ExampleSrv
{
public partial class MyService : ServiceBase
{
public MyService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
}
protected override void OnStop()
{
}
}
}
* This source code was highlighted with Source Code Highlighter.This is, in fact, the service itself.
Use OnStart and OnStop events to implement your task.
Step 2. Adding the installer.
To make your service work, you need to install it.
To install it, it must have an installer.
Right click ... Add installer

Now we have serviceProcessInstaller and serviceInstaller.

In the first you can set the Account value to LocalSystem .
In the second, specify the service name, description and do not forget to put StartType - Automatic .

The installer is ready.
Step 3. Logging.
In order for you to be able to find out what your service was doing, when it started, ended or something else, you can use system logging.
This is done very easily.
Drag from Toolbox to your EventLog service .

This is how logging is done:
public partial class MyService : ServiceBase
{
public MyService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
AddLog("start");
}
protected override void OnStop()
{
AddLog("stop");
}
public void AddLog(string log)
{
try
{
if (!EventLog.SourceExists("MyExampleService"))
{
EventLog.CreateEventSource("MyExampleService", "MyExampleService");
}
eventLog1.Source = "MyExampleService";
eventLog1.WriteEntry(log);
}
catch{}
}
}
* This source code was highlighted with Source Code Highlighter.Step 4. Installation.
To install the service, you need to call the installation utility and pass the path to your service as a parameter.
To do this, I created install.bat of this form: If you selected the Account - User field value in serviceProcessInstaller , then during installation you will have to enter the login and password of the account under which the service will be launched. Attention! Writing a domain before the username is required! We launch the batch file with administrator rights and observe at the end: The Commit phase completed successfully. The transacted install has completed. This means that the service is installed. Check: Installed. We start and stop a couple of times. We look at the logs:
C:\Windows\Microsoft.NET\Framework\v4.0.30319\installutil.exe D:\...\ExampleSrv\bin\Debug\ExampleSrv.exe
pause

We see when the service started and stopped.
Our service is ready.
The material is partially taken from here:
msdn.microsoft.com/en-us/library/zt39148a (VS.80) .aspx