1 new Windows Service project
2 Add custom settings file
3 Configure basic parameters,
You can use the constant Timing_WX_L.Settings1.Default.span defined here in your program as follows,
4 Set installation parameters can refer to http://jingyan.baidu.com/article/fa4125acb71a8628ac709226.html
First, open the Service1.cs view interface
Right---> in the View Add installer
Two, the project adds the ProjectInstaller.cs file, in this file the view will automatically add two components
ServiceProcessInstaller1
ServiceInstaller1
Third, select the ServiceProcessInstaller1 component, view the properties, set account to LocalSystem
Four, in the view right key--> Add Setup program
Five, the project has added the ProjectInstaller.cs file, in which the view automatically adds two components
ServiceProcessInstaller1
ServiceInstaller1
Six, select the ServiceProcessInstaller1 component, view the properties, set account to LocalSystem
Vii. Select the ServiceInstaller1 component to view the properties
Sets the value of the servicename that represents the name in the system service
Set StartType, if manual, start manually, stop by default, if automatic is started automatically
Set description, Add service description
Viii. Rebuilding projects
---------------------------------------------------------------above is the preparatory work--------------------------------------------------- -------------------
5 in the program run the main program, fill in the following procedure
Partial class Wx_pay_l:servicebase {Timer _timer = new timer ();
String FilePath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "logWX.txt";//"D:\\ws.txt";
static int i = 0;
static int span = 0;
Public wx_pay_l () {InitializeComponent (); } protected override void OnStart (string[] args) {//time interval span = Conv Ert.
ToInt32 (Timing_WX_L.Settings1.Default.span); _timer.
Autoreset = true; _timer. Interval = 1000 * span;//default *1000 is 1 seconds _timer.
Elapsed + = new Elapsedeventhandler (ontimedevent);
try {TextWriter sw = new StreamWriter (FilePath, true); Sw.
WriteLine ("------" + System.DateTime.Now.ToString ()); Sw. WriteLine ("service startup.")
"); Sw.
Flush (); Sw.
Close (); The catch {} _timer.
Start ();
private void Ontimedevent (object source, Elapsedeventargs e) {try {
String uri = "Http://localhost:802/api/pay/everyGetAllData";
WebClient WC = new WebClient (); String r = WC.
Downloadstring (URI); catch (Exception ex) {try {TextWriter SW =
New StreamWriter (FilePath, true); Sw.
WriteLine ("------" + System.DateTime.Now.ToString ()); Sw. WriteLine (ex.
message); Sw.
Flush (); Sw.
Close ();
The Catch {}}} protected override void OnStop () {
try {TextWriter sw = new StreamWriter (FilePath, true); Sw.
WriteLine ("------" + System.DateTime.Now.ToString ()); Sw. WriteLine ("Service stoppedCheck.
"); Sw.
Flush (); Sw.
Close (); The catch {}}//private static string Getappconfig (String strkey) Here is a direct reference Webco Nfig//{/foreach (string key in configurationmanager.appsettings)///
if (key = = strkey)//{//return Configurationmanager.appsettings[strkey];
}//}//return null; //}
}
6 after the build of the bin directory there are exe files, and then run the following steps to install and uninstall it for system services
First, click Start, run in the input cmd, get command prompt
Win7 needs to be started as an administrator, otherwise it cannot be installed
Ii. Input CD C:\Windows\Microsoft.NET\Framework\v4.0.30319 carriage return
To switch to the current directory, it should be noted that there are many similar versions in the C:\Windows\Microsoft.NET\Framework directory, specifically to which directory to see the project's operating environment, such as a. NET framework2.0 you need to enter a CD C:\ windows\microsoft.net\framework\v2.0.50727
Third, input InstallUtil.exe E:\TestApp\Winform\WinServiceTest\WinServiceTest\bin\Debug\WinServiceTest.exe carriage return
Description: E:\TestApp\Winform\WinServiceTest\WinServiceTest\bin\Debug\WinServiceTest.exe represents the project-generated EXE file location
Four, open the service, you can see already installed services;
Uninstall is very simple, open cmd, direct input SC delete winservicetest can be. There are also questions you can refer to the reference URL below the document