Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use C # to implement WINDOWS Service Program

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to use C # to achieve WINDOWS service program", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "how to use C # to achieve WINDOWS service program" bar!

Before doing C # WINDOWS services, familiarize yourself with the writing of C # WINDOWS service programs in C #.

1. Create a new project WinSrv_A for the WINDOWS service program in VS.NET2003.

two。 Change the SERVICE1.CS attribute SERVICENAME to the name of the service you want to create. You can see your service name in Service Manager-> name.

3. Press F7 to enter the code window, where there are two overloaded functions in which the one you want to write code is

/ / protected override void OnStart (string [] args) / / {} / / this function is executed after you start the service / / protected override void OnStop () / / {.} / / this function is executed after you stop the service.

I simply do a function here to record the time when the service is started and terminated. Write like a log document.

Protected override void OnStart (string [] args) {/ / TODO: add code here to start the service. String s = System.DateTime.Now.ToString (); if (! File.Exists ("c:\\ srvlog.txt")) {StreamWriter sr = File.CreateText ("c:\\ bbbirdlog.txt") Sr.WriteLine ("- START SRV-"); sr.WriteLine ("my new service starts at {0} time", s) Sr.WriteLine ("I can write integer {0} or floating point {1}, etc.", 1,4.2); sr.Close ();} else {StreamWriter sr = File.AppendText ("c:\\ bbbirdlog.txt") Sr.WriteLine ("- START SRV-"); sr.WriteLine ("my new service starts at {0} time", s) Sr.WriteLine ("I can write integer {0} or floating point {1}, etc.", 1,4.2); sr.Close ();}} / stop this service. / protected override void OnStop () {/ / TODO: add code here to perform the shutdown required to stop the service. String S1 = System.DateTime.Now.ToString (); if (! File.Exists ("c:\\ srvlog.txt")) {StreamWriter sr = File.CreateText ("c:\\ bbbirdlog.txt") Sr.WriteLine ("- STOP SRV-"); sr.WriteLine ("my new service stops at {0} time", S1); sr.Close () } else {StreamWriter sr = File.AppendText ("c:\\ bbbirdlog.txt") Sr.WriteLine ("- STOP SRV-"); sr.WriteLine ("my new service stops at {0} time", S1); sr.Close () }}

4. Go back to the design window point and right-click to add the installer to generate serviceInstaller1 and serviceProcessInstaller1 components

Rewrite the property ServiceName of serviceInstaller1 to your service program name, and set the startup mode to AUTOMATIC

Rewrite the property account of serviceProcessInstaller1 to LocalSystem

5. Compile the link generation service program.

6. Use the. Net framework tool INSTALLUTIL to install the service program.

Thank you for your reading, the above is the content of "how to use C # to achieve WINDOWS service program". After the study of this article, I believe you have a deeper understanding of how to use C # to achieve WINDOWS service program, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report