here is my complet code if it can give you a better view of what i 've done:
namespace testregistre
{
public partial class Service1 : ServiceBase
{
Timer mytimer;
RegistryKey localKey;
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
if (File.Exists(@"C:\temp\journal.txt"))
{
TextWriter file = new StreamWriter(@"C:\temp\journal.txt", true);
file.WriteLine("attention demarrage2 effectué à:" + DateTime.Now.ToString() + "\n");
file.Close();
}
else
{
TextWriter file = File.CreateText(@"C:\temp\journal.txt");
file.WriteLine("attention premier demarrage du service apres deploiement effectué à:" + DateTime.Now.ToString() + "\n");
file.Close();
}
if (mytimer == null)
mytimer = new Timer(15 * 1000.0); //Elapse every 5 seconds and make this configurable. while (1==1) is not the good approach.
mytimer.Elapsed += new ElapsedEventHandler(mytimer_Elapsed);
mytimer.Start();
if (Environment.Is64BitOperatingSystem)
{
localKey = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry64);
}
else
{
localKey = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry32);
}
}
void mytimer_Elapsed(object sender, ElapsedEventArgs e)
{
//Disable timer when we are executing Any particular task. For instance when u write into the text file.
mytimer.Enabled = false;
run();
//Enalbe timer after the test execution is done
mytimer.Enabled = true;
}
static void run()
{
try
{
int cle = (int)Registry.GetValue(@"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Service\USBSTORE", "start", -1);
// if (cle == 3)
// {
TextWriter file = new StreamWriter(@"C:\temp\journal.txt", true);
file.WriteLine("lavaleur du registre de la cle usb est: " + cle + "\n");
file.Close();
// }
}
catch {
TextWriter file = new StreamWriter(@"C:\temp\journal.txt", true);
file.WriteLine("acces impossible au registre " + "\n");
file.Close();
}
}
protected override void OnStop()
{
if (mytimer != null)
{
mytimer.Stop();
mytimer.Dispose();
}
TextWriter file = new StreamWriter(@"C:\temp\journal.txt", true);
file.WriteLine("attention arretttttt effectué à:" + DateTime.Now.ToString() + "\n");
file.Close();
}
}
}
i m really sorry to take some of your time
thank you for your help
Tobi, if this helps please login to Mark As Answer. | Alert Moderator