Batch script to install or uninstall A. Net Windows Service

Source: Internet
Author: User

For Windows batch files, the extension of the batch files is. bat or. cmd. When the command line prompts the name of the batch file, the system will call cmd.exe to execute commands in sequence in the batch file.

1.
Run cmd
Type the command echo Hello world.
Result: Hello World

ECHO is an output command, which is equivalent to console. writeline ("Hello World ")

2.
Helloworld. bat
Echo Hello World

Double-click to run
Result: The command is aborted because the execution of the command is complete.
Add a pause command to suspend the operation.

3.
Helloworld. bat
Echo Hello World
Pause

Result:
Echo Hello World
Hello World
Press any key to end (I will omit it later)

Cmd.exe displays every command we read, so the echo Hello world mission we execute is also displayed. (Called Echo)
Sometimes we don't want users to know which commands we execute, so we can disable echo.

4.
Helloworld. bat
@ Echo Hello World
Pause

Double-click to run
Result:
Hello World

Note: you can close the echo of the current command. If there are 100 commands, add them before each command.

5.
Helloworld. bat
@ Echo off
Echo Hello World
Echo Hello World
Pause

Double-click to run
Result:
Hello World
Hello World

Note: @ echo off can disable ECHO for all commands

---------------------------------------------------------------- The following section describes the batch commands used.

Create a Windows Service Project and add helloworldservice

Program. CS code:

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. serviceprocess;
Using system. text;

Namespace helloworld
{
Static class Program
{
/// <Summary>
/// The main entry point for the application.
/// </Summary>
Static void main ()
{
Servicebase [] services = new servicebase [] {
New helloworldservice ()
};
Servicebase. Run (services );
}
}
}

To run servicebase in system. serviceprocess, we need to run the service on Windows.

Helloworldservice. CS code:

Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. diagnostics;
Using system. LINQ;
Using system. serviceprocess;
Using system. text;

Using system. IO;
Using system. Threading;

Namespace helloworld
{
Public partial class helloworldservice: servicebase
{
Public helloworldservice ()
{
Initializecomponent ();
}

Private thread;

Protected override void onstart (string [] ARGs)
{
Directory. setcurrentdirectory (appdomain. currentdomain. basedirectory );
Log ("START helloworldservice ");

Thread = new thread (New threadstart (run ));
Thread. Priority = threadpriority. normal;
Thread. Start ();
}

Private Static void run ()
{
While (true)
{
Log (datetime. Now. tostring ());
Thread. Sleep (1000 );
}
}

Protected override void onstop ()
{
Log ("Stop helloworldservice ");
}

Private Static void log (string message)
{
String Path = string. Format (@ "{0} \ {12.16.txt ",
Directory. getcurrentdirectory (),
Datetime. Today. tostring ("yyyy-mm-dd "));

Using (streamwriter = new streamwriter (path, true ))
{
Streamwriter. writeline (Message );
}
}
}
}

Service must inherit from system. servicebase in serviceprocess, and appdomain in Windows service. currentdomain. basedirectory, directory. getcurrentdirectory () and so on will be jumped to the c: \ windows \ system32 directory by default, so we need to set currentdirectory in onstart.

-------------------------------------------------------------- Batch File Processing
Install. bat

@ Echo off
Echo install start ....
C: \ windows \ Microsoft. NET \ framework \ v2.0.50727 \ installutil.exe/I helloworld.exe
Net start "helloworldservice"
Pause

Here, the program helloworld.exe is installed in/I kernel. Net start indicates that helloworldservice is started.

Uninstall. bat
@ Echo off
Echo uninstall start ....
C: \ windows \ Microsoft. NET \ framework \ v2.0.50727 \ installutil.exe/u mailsender.exe
Pause

Here/u uninstall the helloworld.exe program. net stop will not be used and will not be uninstalled.

-------------------------------------------------------------- Possible problems
1. Unable to install, prompt runinstaller (true )...
Cause: the installation package is not provided. Right-click Add installer on the designer of helloworldservice and a project Installer. CS file will be generated.

2. Open the projectinstaller. CS designer.
By default, the helloworldservice installation instances helloworldserviceinstaller and serviceprocessinstaller will be added, and their names will be retrieved by themselves...

3. helloworldserviceinstaller setting starttype to Automatic startup is convenient.

4. The service is started by default. The user logon box is displayed and needs to be verified.
Set the account of serviceprocessinstaller to LocalSystem

--------------------------------------------------------------- Problems that may occur in Windows 7
Double-click the instal. bat batch file and the system. Security. securityexception message is displayed, prompting to throw the event log creation (security exception, usually caused by insufficient permissions)
This exception occurs when we run this batch file because we do not have the permission to create EventLog.

No EventLog code is created in helloworldservice, so possible problems should be found in servicebase.
Check servicebase and find that the EventLog code is used. We can see the relevant information in the event viewer. In Windows 7, non-administrators do not have this permission.
We have used log by ourselves, so EventLog is no longer necessary. You can set autolog to false without logging. (However, if it is set, the result is the same, and the EventLog will still be created. I don't know how to turn it off .)

This batch can only be used to execute the batch file in the form of a manager, and then change helloworld.exe in the batch file to an absolute path.

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.