1. First open VS2010 (or other version) to create a Windows service project
2. After the creation is switched to Code view, the code defaults to the OnStart and OnStop methods to perform service opening and service stop execution, the following code is explained in detail:
Note that the system time is selected, not the time in the WinForm.
Using System;
Using System.IO;
usingsystem.serviceprocess;
Using System.Text;
Usingsystem.timers;
Namespacetestservice
{
public partial class Service1:servicebase
{
Public Service1 ()
{
InitializeComponent ();
}
protected override Voidonstart (string[] args)
{
Service Open Execution Code
Startdosomething ();
}
protected override void OnStop ()
{
Service End Execution Code
}
protected override void OnPause ()
{
Service paused execution Code
Base. OnPause ();
}
protected override void OnContinue ()
{
Service Recovery Execution Code
Base. OnContinue ();
}
protected override void OnShutdown ()
{
System is about to close execution code
Base. OnShutdown ();
}
private void Startdosomething ()
{
System.Timers.Timer Timer = NewSystem.Timers.Timer (10000); Interval 10 seconds
Timer. AutoReset = true; boot start
Timer. Enabled = false; Executes once, true when no 10S is executed.
Timer. Elapsed + = new Elapsedeventhandler (writesomething);
Timer. Start ();
}
private void Writesomething (Objectsource, System.Timers.ElapsedEventArgs e)
{
FileStream fs = null;
Try
{
FS = new FileStream ("D:/1.txt", FileMode.OpenOrCreate);
String strText = @ "//instantiate a file stream---> associated with Write file
FileStream fs = new FileStream (SF. FileName, FileMode.Create);
Instantiate a streamwriter--> associated with FS
StreamWriter SW = new StreamWriter (FS);
Start writing
Sw. Write (This.textBox1.Text);
emptying buffers
Sw. Flush ();
Close the stream
Sw. Close ();
Fs. Close (); ";
Get byte array
byte[] data = new UTF8Encoding (). GetBytes (StrText);
Start writing
Fs. Write (data, 0, data. Length);
Empties the buffer, closes the stream
Fs. Flush ();
Fs. Close ();
Fs. Dispose ();
}
Catch
{
}
Finally
{
if (fs! = NULL)
{
Fs. Close ();
Fs. Dispose ();
}
}
}
}
}
3. Then switch to Design view, right-click on the "Add Installer" in circle
4. Select the first control, click F4, switch to the property view on the right, change the account property in the Properties view to LocalService (Local service)
5. Select the second control above, click F4, and switch to the property view on the right. Change ServiceName to your own favorite service name, remember not to conflict with the system, starttype default to Manual, you may change to automatic
(Automatic) or disabled (Disabled)
6. Compile the project, then win+r enter cmd into the command window. To find InstallUtil.exe in the directory corresponding to the. NET version, my project uses. NET 4.0, so the path is C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319
7.installutil.exe corresponds to the. NET version directory diagram, as follows
8. Then win+r enter cmd into the command window.
9.
Method Delete Service: Direct registry edit open Registry Editor and locate the following key value:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services General Service will show a key in the same name here, directly delete the relevant keys can be.
Create a simple process for Windows services