C # procedure for compiling a Windows Service Program

Source: Internet
Author: User

 1. Create a project, or right-click the current solution and choose add to create a project.

2. select (project type) Visual C # project, (Template) Windows Service, fill in the name of the service to be created (change the default windowservice1 to your own service name), and then press OK.

3. A windowsservice1 project is created by default in resource manager. The project contains a service1.cs file.

4. select "toolbox" -- "component" on the left, and drag a timer object to the form of service1.cs [design. click timers1 on the form to set its attribute: interval attribute number N (for example, 200 ms = 1 second for 5 Operations)

5. double-click the timer1 component and add the required function
private void timer1_elapsed (Object sender, system. timers. elapsedeventargs e)
{< br> // hardware configuration file of the Local Computer
string strfilename = @ "C:/computerinfo.txt ";
string strtest = "Hard Disk information + video card information + motherboard information + sound card information + Nic information + printer information";
// determines whether the transfer file exists. if not, write a row of test information
If (! File. exists (strfilename)
{< br> file. createtext (strfilename);
}< br> byte [] content = encoding. utf8.getbytes (strtest);
// create a text file object
filestream FS = new filestream (strfilename, filemode. openorcreate, fileaccess. readwrite);
// write test data to the file
FS. write (content, 0, content. length);
// close the file object
FS. close ();
}

6. Add the corresponding functionCodeSwitch from the Code view to the design view, right-click and choose "add installation"Program"

7. in the current project resource manager, a project installer is added. CS file, click the file to open projectinstaller. CS [design] form. there are two components "serviceprocessinstaller1" and "serviceinstaller1"

8. Set the attributes of the serviceinstaller1 component: servicename = myservice (the name can be changed at Will) and starttype = automatic (the automatic running mode upon startup)

9. Set the attribute of the serviceprocessinstaller1 component: account = LocalSystem

10. select the current solution and set "Start Project" in the solution properties to the name of the project you just added. press F5 to run and compile the current project. A dialog box is displayed, "you can run the firewall to start the service. First, install Windows Service (use installutil.exe), and then start it with Server Explorer, Windows service management tool, or net start command ". The error does not matter, as long as an EXE file is added under the current solution/project folder/bin/debug/folder.

At this point, the basic operation of the service you need has been completed. The rest is to compile. Register and start the service.

11. Windows Server registration is stopped
Open the CMD window -- start -- run -- Enter cmd and press Enter.
Register Service commands
Cd c:/Windows/Microsoft. NET/framework/v1.1.4322
Installutil D:/hardwareserverservice.exe
Nets tart computerservice

Re-Register Service
Cd c:/Windows/Microsoft. NET/framework/v1.1.4322
Installutil/u d:/hardwareserverservice.exe
From your C # resolution, upload the file "hardwareserverservice.exe" to the disk" hardwareserverservice.exe ".
Installutil D:/hardwareserverservice.exe
Net start computerservice

Where D:/hardwareserverservice.exe can be changed to "your current solution directory/project name/bin/debug/hardwareserverservice.exe"

12. start the just-registered service: display the desktop-right-click "my computer" on the desktop and select "manage" to access the computer management console, select "service" in "service and application", and find the newly registered computerservice in the service list on the right (the status is stopped by default ), select this service, right-click it, and start the selected computerservice to complete all operations. Then, the service will automatically start and run after each boot.

13. A problem after registration
after the machine is restarted, it is found that the local SQL Server database cannot be connected and the database server is not started, after manual startup, the system will prompt the error "sqlserver2000 Error 1069 -- unable to start the service due to login failure ". Solution: Start-> Administrative Tools-> services, find the MSSQLServer Service, right-click Properties-> log on, select this account as the login identity: Enter the user name. /administrator, re-enter the password and confirm the password, click OK to return. solve the problem!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.