Wheel _windows SC and NET commands for service operations

Source: Internet
Author: User
Tags net command

Look at a chestnut:

1. "New Project"--"window service"

The resulting directory structure

Double-click "MainService.cs", right click "Add Installer", automatically generate Projectinstaller.cs file and two installation components, rename the two components and do property settings:

Adding code to the start and stop of a service

usingSystem;usingSystem.Diagnostics;usingSystem.IO;usingsystem.serviceprocess;usingsystem.timers;namespacemainservice{ Public Partial classMainservice:servicebase {PrivateTimer time =NewTimer ();  PublicMainservice () {InitializeComponent (); }        protected Override voidOnStart (string[] args) {Writelog ("service Start, Time:"+ DateTime.Now.ToString ("HH:mm:ss") +"\ r \ n"); Time. Elapsed+=NewSystem.Timers.ElapsedEventHandler (methodevent); Time. Interval=2* +;//time interval is 2 secondsTime .        Start (); }        protected Override voidOnStop () {Writelog ("Service stopped, time:"+ DateTime.Now.ToString ("HH:mm:ss") +"\ r \ n"); }        Private voidMethodevent (Objectsource, System.Timers.ElapsedEventArgs E) {time. Enabled=false; stringresult =string.            Empty; stringStartTime = DateTime.Now.ToString ("HH:mm:ss"); Try            {                //.........result="successful execution, time:"+StartTime; }            Catch(Exception exp) {result="failure, Reason:"+exp.            Message; }            finally{writelog (result); Time. Enabled=true; }        }        /// <summary>        ///Log Records/// </summary>        /// <param name= "Loginfo" ></param>         Public voidWritelog (stringloginfo) {            Try            {                stringLogdirectory = AppDomain.CurrentDomain.BaseDirectory +"\\Logs"; if(!directory.exists (logdirectory))                {directory.createdirectory (logdirectory); }                stringFilePath = Logdirectory +"\\"+ DateTime.Now.ToString ("YYYY-MM-DD") +". txt";            File.appendalltext (FilePath, loginfo); }            Catch            {            }        }    }}
View Code

Add a Windows Forms application named: Installservice

To add code to the startup program:

usingSystem;usingSystem.Collections.Generic;usingSystem.Diagnostics;usingSystem.Linq;usingSystem.Text;namespaceinstallservice{classProgram {Static voidMain (string[] args) {Console.WriteLine ("This is the Windows application"); Console.WriteLine ("Please select, [1] Install service [2] Uninstall service [3] Exit"); varrs =int.            Parse (Console.ReadLine ()); Switch(RS) { Case 1:                    //take the current executable path, plus the "s" parameter, which proves to be starting the program from the Windows service                    varPath =process.getcurrentprocess ().                    Mainmodule.filename; Process.Start ("SC","create myserver binpath= \ ""+ Path +"\ "Displayname=mainservice start= auto"); //Since the SC command was installed and did not see the description, I suspect that the installed service is not necessarily the Mainservice service project I created, which adds a descriptionProcess.Start ("SC","description myserver \ "Uses idle network bandwidth to transfer files in the background. If the service is deactivated, such as Windows Update and MSN Explorer features, the program and other information will not be downloaded automatically. If this service is disabled, any service that relies on it does not have fault-tolerant technology to transfer files directly through IE, and once BITS is disabled, the file may not be transferred. \""); Console.WriteLine ("Installation Successful");                    Console.read ();  Break;  Case 2: Process.Start ("SC","Delete MyServer"); Console.WriteLine ("Uninstall succeeded");                    Console.read ();  Break;  Case 3:                      Break; }        }    }}

Start the program:

1 after installation

Run "services.msc" to open Service Manager

I failed to start manually

So I suspect that I installed just a name that has nothing to do with the service I added

Option 2

Uninstall succeeded

Can see net named installation and uninstall see: http://www.cnblogs.com/qk2014/p/4869675.html

The Windows Service Operations Command has the SC and net two commands;

Using the SC command:

1 Modifying the service startup type

sc Config Service name start= demand (set service to start manually)

start= Auto (set service to start automatically)

start= Disabled (set service to Disabled)

2 Installation Services: SC Create service Name

3 Stop service: SC stop service Name

4 Uninstall Service: SC Delete Service name (stop service before uninstalling)

Using the net command

To view running services: NET start

Start Service: Net start service name

Stop service: net stop service name

Uninstall Service: Service name-uninstall (stop service before uninstalling)

Although 2 can reach the stop service and start the service effect, but when restarting the service, is the use of batch first stop after start to achieve a restart, if the service is in the start state using SC stop after start, the result service cannot start, The start command cannot be started because the SC command does not wait for the execution of the batch to proceed immediately after the stop operation, when the service is in a stopped state.

NET command stops waiting at stop, and after the service has stopped completely before continuing with the statement after the batch, the net restart command is normal.

Site:

Http://www.cnblogs.com/testlife007/p/4396248.html

Http://www.cnblogs.com/DelphiFan/archive/2009/03/10/1645579.html

Http://www.cnblogs.com/a311300/archive/2010/12/30/1922229.html

Http://www.cnblogs.com/wpcnblog/p/4732390.html

Http://www.cnblogs.com/zcy-628/archive/2012/08/22/2650805.html

Http://www.cnblogs.com/qk2014/p/4869675.html

Http://www.cnblogs.com/smileberry/p/4210245.html

The SC and net commands for wheel _windows service operation

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.