Friday, January 11, 2008

How to check the check box "Allow service to interact with desktop" programatically in .Net

Question:
For a Windows Service How to check the check box "Allow service to interact with desktop" programatically using .Net

Solution:
Its little bit tricky to do this. To achieve this you must deal with Windows Registry of the Local machine where exactly the Service is running. Add the following code snippet and call before the service OnStart() Method. Thats all!

Microsoft.Win32.RegistryKey seviceKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Services\YourServiceName", true);
if (seviceKey != null)
{
if (seviceKey.GetValue("Type") != null)
seviceKey.SetValue("Type", ((int)seviceKey.GetValue("Type") | 256));
}


With regards,
Francis Michael.

No comments: