I had made a windows service in visual studio 2008 in C#. inside the service i had written only single line code
try
{
System.Diagnostics.Process.Start(@"E:\Users\Sk\Desktop\category.txt");
}
catch { }
then i add the project installer & change the serviceProcessInstaller1 Account property as local system Also change the serviceInstaller1 start type property as Automatic. then i build 开发者_如何学Pythonthe project.it was successful. after that i add another project that was setup project.i had added primary project output & i had added the custom action as "Primary output from DemoWindowsService (Active)".then built the setup.setup was build successfully.then i install the setup & then went to services start the service.service stated properly but it was not performing the task. i had checked the path is correct & also i tried to do
System.Diagnostics.Process.Start(@"E:\Windows\system32\notepad.exe")
but still result is same.i tried a lot but not getting the answer.
Windows services cannot interact with the desktop by default, and Notepad is an interactive application.
If you open Task Manager, I suspect you will see the notepad.exe
process running after you started your service, and that your service is doing what you think, it's just not visible.
You can change a service to interact with the desktop by right-clicking it in the Services MMC snap-in (Start -> Run -> Services.msc
), and checking the Allow service to interact with desktop option on the Log On
tab.
Relying on this option is not recommended though, as Microsoft have said they will remove it in later versions of Windows.
It makes sense if you think about it, since a Windows service is meant to be usable even if no-one is logged on to the computer (e.g. there is no "desktop" to interact with).
If it's not working, then maybe it's because an exception was thrown. However, since you're hiding all exceptions with your try/catch block, you'll never know what's wrong.
Get rid of the try/catch block, and see if that helps you learn what's wrong.
Have you written your single line of code in the OnStart() method in the service?
If you did, then change your catch block to catch (Exception ex)
and write the exception details to the eventlog to see why the exception occurred.
See Logging Information About Services
精彩评论