1

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?

  • 2
    A service by design doesn't have access to a users desktop. a service runs before a user is logged in is purposely locked down for security reasons. no apps really do this anymore and you should probably consider an autorun app that lives in a the task tray – TheGeneral Aug 21 '18 at 03:11
  • Or you could use simple console app and Task Scheduler. Found similar thread that might be helpfull. https://stackoverflow.com/questions/5307968/how-can-i-run-an-exe-program-from-a-windows-service-using-c – Roman Svitukha Aug 21 '18 at 03:19
  • What @RomanSvitukha said – TheGeneral Aug 21 '18 at 03:19

0 Answers0