I'm trying to write a Windows service that opens a program when it starts/restarts. I doubt it makes a difference but it's a spreadsheet program like Excel just in case it's important to this task. It's located at the root of my D: drive. Process.Start works just fine in this console program (so I know I'm using it right):
namespace Example
{
public class Program
{
public static void Main()
{
System.Diagnostics.Process.Start(@"D:\program.exe");
}
}
}
I followed this guide: https://learn.microsoft.com/en-us/dotnet/framework/windows-services/walkthrough-creating-a-windows-service-application-in-the-component-designer to write my own service. I have that exact same line in the OnStart method of my service project as seen here:
protected override void OnStart()
{
System.Diagnostics.Process.Start(@"D:\program.exe");
}
I opened the install utility as administrator and successfully built and installed as described in the guide. Nothing was happening when I started the service, so I looked up how to debug a service. I changed my OnStart method to:
protected override void OnStart()
{
System.Diagnostics.Debugger.Launch();
System.Diagnostics.Process.Start(@"D:\program.exe");
}
While stepping through my code, I could see Process.Start threw an InvalidOperationException error. I read up on it (https://learn.microsoft.com/en-us/dotnet/api/system.invalidoperationexception?view=netframework-4.7.2) but I'm stuck as to how to resolve it. I tried the local user, local service, and system service accounts when building and installing, but none helped. Has anyone launched executables in a service?