Created service:Winservicedemo
1. Create a Windows service project, such as mywinservice.CodeImplement the functions you want to implement. As follows:
View plaincopy to clipboardprint?
String filename = @ "E: \ test \ winservice. log ";
Protected override void onstart (string [] ARGs)
{
// Todo: Add code here to start the service.
File. appendalltext (filename, "winservicedemo started", encoding. Default );
}
Protected override void onstop ()
{
// Todo: Add code here to stop the service.
File. appendalltext (filename, "winservicedemo stopped", encoding. Default );
}
2. Right-click the blank area on the winservicedemo design page, select "properties", and setSet the servicename attribute to "winservicedemo ",
3. Right-click the blank area on the winservicedemo design page and select"Add and installProgram", Go to the" projectinstaller design "view, select" serviceinstaller1 ", and set its servicename attribute to" winservicedemo ". Note: it must be the same as the servicename set in step 2. Otherwise, after packaging and installation, the service cannot be started normally. Other attributes, such as description, displayname, and starttype, are displayed on your own. If there are multiple services in this project, you need to set modifiers to public. Otherwise, the service cannot be started normally after packaging and installation.
Right-click servicepprocessinstaller1 in the project installer design view and select Properties.Account: LocalSystem
5. If the project contains multiple services, such as winservicedemo and winservice2, you also need to modify programs,
View plaincopy to clipboardprint?
Static void main ()
{
Servicebase [] servicestorun;
Servicestorun = new servicebase []
{
New winservicedemo (),
New winservice2 ()
};
Servicebase. Run (servicestorun );
}
6. Add a deployment project, such as windowsservicesetup.
7. Right-click the windowsservicesetup project, add -- project output, select the project as the main Windows Service Project to be deployed, such as "winservicedemo", and select "main output ",
8,Right-click the windowsservicesetup project, view --- custom operations, right-click the custom operations and add custom operations in the Custom operations window ", select "Application folder" ---- "main output comes from ...... (activity )",
9. Compilation and generation