Kevin Schultz
Kevin Schultz

Reputation: 884

Launch an executable from within a windows file watcher service

I have a file watch service, written in C#, that I need to launch an application when it detects the file drop. I am using notepad as a test application to launch. The file watcher is working fine, but I cant get notepad to launch. Any assistance with what I am missing would be great.

Code that fires when the file drop is detected:

public void FileCreated(object source, FileSystemEventArgs inArgs)
        {
            Process LaunchApp = new Process();
            LaunchApp.StartInfo.FileName = ConfigurationManager.AppSettings["AppStartPath"];
            LaunchApp.Start();
           // Process.Start(ConfigurationManager.AppSettings["AppStartPath"]);
            Log.WriteLine(" File added: " + DateTime.Now + " " + inArgs.FullPath);
        }

Path reference from the app.config:

<add key="AppStartPath" value="Notepad.exe"/>

I also tried:

<add key="AppStartPath" value="C:\Windows\System32\Notepad.exe"/>

Upvotes: 0

Views: 309

Answers (1)

Richard
Richard

Reputation: 109140

I have a file watch service,

Services run in a separate security context to processes in a user logon session.

This can be seen if you add the Session ID column to Task Manager's Processes tab, or – better, in Process Explorer.

Any processes the service launches will run in the service's own context: not the user. There are very good security reasons for this.

To perform interactive operations from a service you need a per user agent that is run in the user's context. Typically the service listens on a named pipe and the user agent is run from the startup group (or Run key in the registry). The agent connects to the named pipe and can respond to requests from the service (or the service from the user agent).

Upvotes: 1

Related Questions