using System;
using System.IO;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Configuration.Install;
using System.Windows.Forms;
namespace testdienst
{
/// <summary>
/// This is the class for my Service
/// </summary>
public class MyService : System.ServiceProcess.ServiceBase
{
public MyService()
{
InitializeComponents();
this.ServiceName = "TestDienst";
ProjectInstaller pi = new ProjectInstaller();
// TODO: Add any further initialization code
}
private void InitializeComponents()
{
this.ServiceName = "TestDienst";
}
/// <summary>
/// This method starts the service.
/// </summary>
public static void Main()
{
System.ServiceProcess.ServiceBase.Run(new System.ServiceProcess.ServiceBase[]
{
new MyService() // To run more than one service you have to add them here
});
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose(bool disposing)
{
// TODO: Add cleanup code here (if required)
base.Dispose(disposing);
}
/// <summary>
/// Start this service.
/// </summary>
protected override void OnStart(string[] args)
{
StreamWriter sw;
int i = 1;
if ( !File.Exists("c:\test.txt"))
{//*** Wenn die Datei für den Test nicht existiert, dann soll die neu angelegt werden
sw = new StreamWriter(File.Open("c:\test.txt", FileMode.Create));
sw.WriteLine("Das ist ein Test");
sw.Close();
}
//*** Hier wird ein Zähler von 1 bis 1000 in die Datei geschrieben
sw = new StreamWriter(File.Open("c:\test.txt", FileMode.Append));
while (i<1000)
{
sw.Write(i.ToString() + " ");
i++;
}
sw.Close();
}
/// <summary>
/// Stop this service.
/// </summary>
protected override void OnStop()
{
// TODO: Add tear-down code here (if required)
// to stop your service.
}
}
}
[RunInstaller(true)]
public class ProjectInstaller : Installer
{
ServiceInstaller si = new ServiceInstaller();
public ProjectInstaller()
{
ServiceProcessInstaller spi = new ServiceProcessInstaller();
spi.Account = ServiceAccount.LocalSystem;
ServiceInstaller si = new ServiceInstaller();
si.ServiceName = "TestDienst";
si.StartType = ServiceStartMode.Automatic;
Installers.AddRange(new Installer[] {spi, si});
}
public override void Install(IDictionary stateServer)
{
base.Install(stateServer);
Microsoft.Win32.RegistryKey desc;
try
{
desc = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(
"System\CurrentControlSet\Services\" + si.ServiceName, true);
desc.SetValue("Description", "Das ist ein erster Testdienst");
desc.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
protected override void OnAfterInstall(IDictionary savedState)
{
base.OnAfterInstall(savedState);
Microsoft.Win32.RegistryKey image;
try
{
image = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(
"System\CurrentControlSet\Services\" + si.ServiceName, true);
si.ServiceName = "TestDienst";
image.SetValue("ImagePath", image.GetValue("ImagePath") + " - Service");
image.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}