I am trying to create a service that opens an application, however I was not lucky. Thus, I downloaded the following code sample and tried to create a service based on it. However, this does not work either. It happens that the code is executed, but the executable file is never called (in the following case, the calculator does not open).
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
namespace Open_Calculator
{
public partial class Service1 : ServiceBase
{
public static Process process;
public Service1()
{
string[] args = { "1", "2" };
OnStart(args);
}
protected override void OnStart(string[] args)
{
start_calc();
}
protected override void OnStop()
{
}
static protected void start_calc()
{
process = new Process();
process.StartInfo.FileName = @"C:\Windows\system32\calc.exe";
process.StartInfo.CreateNoWindow = true;
process.StartInfo.ErrorDialog = false;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
process.Start();
}
}
}
The exact same code copied to the console application works.
The service is configured to interact with the desktop, and it is configured to start as "LocalSystem".
The account on which the service is running has access to C:\Windows\System32when I tested it. Process.Start () does not return any errors, and calc does not work in the background (checked by the task manager)