Windows Services are called NT Services in earlier versions of Visual Studio, and new names are enabled in VS.net. Using Visual C # To create a Windows service is not difficult. This article will guide you step by step to create a Windows service and use it. When the service starts and stops, it writes some text information to a text file.
Step 1: create a service framework
To create a new Windows Service, you can select the Windows Service option from the Visual C # project, give the project a new file name, and click OK.
You can see that the wizard adds the WebService1.cs class to the project file:
The meaning of each attribute is:
Whether Autolog is automatically written to the System Log File
Receive Power Events During CanHandlePowerEvent Service
Whether the CanPauseAndContinue Service accepts the pause or continue running request
Whether the CanShutdown Service receives a notification when the computer that runs it shuts down, so that it can call the OnShutDown Process
Whether the CanStop Service accepts the stop operation request
ServiceName service name
Step 2: add features to the service
Add using System. IO to the header;
In the. cs code file, we can see that there are two ignored functions OnStart and OnStop.
The OnStart function is executed when the service is started, and the OnStop function is executed when the service is stopped. Here, when the service is started and stopped, some text information is written to a text file. The Code is as follows:
Protected override void OnStart (string [] args)
{
// TODO: Add code here to start the service.
FileStream fs = new FileStream (@ "d: mcWindowsService.txt", FileMode. OpenOrCreate, FileAccess. Write );
StreamWriter m_streamWriter = new StreamWriter (fs );
M_streamWriter.BaseStream.Seek (0, SeekOrigin. End );
M_streamWriter.WriteLine ("mcWindowsService: Service Started" + DateTime. Now. ToString () + "");
M_streamWriter.Flush ();
M_streamWriter.Close ();
Fs. Close ();
}
Protected override void OnStop ()
{
// TODO: Add code here to stop the service.
FileStream fs = new FileStream (@ "d: mcWindowsService.txt", FileMode. OpenOrCreate, FileAccess. Write );
StreamWriter m_streamWriter = new StreamWriter (fs );
M_streamWriter.BaseStream.Seek (0, SeekOrigin. End );
M_streamWriter.WriteLine ("mcWindowsService: Service Stopped" + DateTime. Now. ToString () + "");
M_streamWriter.Flush ();
M_streamWriter.Close ();
Fs. Close ();
}
Step 3: add the installer to the Service Application
Visual Studio. NET comes with the installation component that can be used to install resources associated with service applications. The installation component registers a single service on the system to which it is being installed, and notifies the Service Control Manager of the existence of the service.
To correctly install the service, you do not need to perform any special encoding in the installer. However, to add special features to the installation process, you may occasionally need to modify the content of the installation program.
To add an installer to a service application, follow these steps:
1: In the solution, access the Design view of the service to which you want to add and install components.
2: In the Properties window, click the "add installer" link. If not, right-click in the gray help area under the property, select "command" and click "add installer.
In this case, a new ProjectInstaller class and two installation components ServiceProcessInstaller and ServiceInstaller are added to the project, and the service attribute values are copied to the component.
3: To determine how to start the service, click the ServiceInstaller component and set the StartType attribute to an appropriate value.
After the Manual service is installed, it must be started manually.
The service is automatically started every time the computer restarts.
The Disabled service cannot be started.
4: Change the Account attribute of the serviceProcessInstaller class to LocalSystem.
In this way, no matter which user is logged on to the system, the service will always start.
Step 4: generate a service program
You can select generate from the generate menu to generate a project.
Do not run the project by pressing F5-you cannot run the service project in this way.
Step 5: Install the service
Access the directory of compiled executable files in the project.
Use the project output as a parameter to run InstallUtil.exe from the command line. Enter the following code in the command line:
Installutil yourproject.exe
Note: InstallUtil.exe has two versions, which are located on my machine:
C: WINDOWSMicrosoft.NETFrameworkv1.1.4322InstallUtil.exe 16 K for. net1.1
C: WINDOWSMicrosoft.NETFrameworkv2.0.50727InstallUtil.exe 26 K for. net2.0
Uninstall Service
Use the project output as a parameter to run InstallUtil.exe from the command line.
Installutil/u yourproject.exe
An error may occur if the file is changed to a folder. You can enter the SC delete service name in CMD to delete the service.
Or
Build the reg. bat file with the following content:
Registration Service
%Windir%Microsoft.NETFrameworkv2.0.50727InstallUtil.exe unzip cd#sendemail.exe
Net start SendEmail
Uninstall the service:
The new unload. bat file contains the following content:
Net Stop SendEmailNoBrokeInfo
%Windir%Microsoft.NETFrameworkv2.0.50727InstallUtil.exe/u %CD%SendEmail.exe