C # Creating a Windows service Getting Started illustration (VS2010)

Source: Internet
Author: User

C # Creating a Windows service Getting Started illustration (VS2010) Windows services Everyone knows, such as audio, theme are more familiar with the service, they can be set to start automatically, and in the registry of the boot-up item is no trace. So used to hide the Trojan. But the old Trojan service was done by hand-guided registry. There has never been a formal development of a service.

In a previous project, someone used a service method to get remote data regularly, very sharp, I feel very useful. So today I have a wasteland. Develop Windows services in C #, and have a formal experience with the development process. Here are some of the most introductory examples I've done, and there's a similar tutorial on MSDN, but MSDN misses some of the more important steps, so let's see the successful version of my Wasteland:

First, create a Windows service project in VS2010:

It will provide us with a default Serivce1 that can be deleted and replaced by your own. For example, Fucker.cs, of course, you have to replace the statement that is called in Program.cs:

Static void Main(){ servicebase[] servicestorun;servicestorun = new< Span class= "PLN" > servicebase[]{ new fucker ()  };  servicebase. Runservicestorun               /span>                

After creating the Fucker.cs, open the Design view and drag a eventlog from the toolbox, which is in the components category. Note that you cannot use Windows Form controls, or the compilation will explode. If your service wants to pop up the message in the UI, you can only put in the build folder a specially developed EXE that specializes in playing things ...

Now, you can code this service. First add a few words to the constructor:

Public Fucker(){ InitializeComponent(); If (!System.Diagnostics.EventLog.SourceExists("Thefuckers")) { system. Diagnostics. Eventlog. Createeventsource ( "thefuckers" ,  "Fuckerlog" );  } Evt. Source =  "thefuckers"  Evt. Log =  "Fuckerlog" ;< Span class= "PLN" >}            

First of all to judge the system of the EventLog there are "thefuckers" This classification, if the wood has, build one. The following parameter "Fuckerlog" is the name of the log. Next we will write the log when the service starts and stops:

Protected Override void onstart (string[] args { Evt. Writeentry ( "Oh fuck!" }protected  void onstop () { Evt. Writeentry ( "Oh shit!" }             /span>                

Now, Fucker's service is finished. We need to add an important installer, or we won't be able to install it. The method is to right-click on the Design view of fucker and choose Add Installer:

And then we're going to code a bit ProjectInstaller.Designer.cs, note that it's Designer.cs, the designer-generated code file, not the ProjectInstaller class that's right-clicking on the Design view.

First of all to remove the user name and password, or the installation will ask you to, it will use this account to start the service, if your account does not have permissions, the service is to install the failure. Comment out the following code:

This.serviceProcessInstaller1.Password = null;  //this.serviceProcessInstaller1.Username = null;

Then switch to the LocalSystem identity to start the service:

This.  ServiceProcessInstaller1.  Account=System.  ServiceProcess.  ServiceAccount.  LocalSystem;            

If you want to change the name of the service (that is, the name you see in the Service Manager), you can modify the ServiceName property:

This.  ServiceInstaller1.  ServiceName="Fucker";      

Now that the Code section is finished, finally change the option of start up in the project properties:

Then compile, and after success, you will generate an EXE in the Bin folder.

Then you can install it with Installutil.exe in the command-line tool that comes with VS2010:

D:\dotnetprojects\testlab\showfuck\bin\debug>installutil.  Showfuck.  EXE         

A message is displayed when the installation succeeds:

Now go to the Windows Service Manager to see our services:

Try to start and stop the service, we can see the fuck in the event log, haha.

 

C # Creating a Windows service Getting Started illustration (VS2010)

Related Article

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.