Running a batch file from a .NET 6.0 Windows Service in a window

Viewed 34

This is supposed to be simple but I don't understand why it's not working. I'm trying to run a batch file (that runs a console application) from a .NET 6.0 Windows Service with this content:

@echo off
cd "C:\MyFolder"
MyExecutable.exe
pause

and I have this code:

var command = "\"C:\\Users\\Administrator\\Desktop\\mybatchfile.bat\"";
ProcessStartInfo start = new()
{
    FileName = "cmd.exe",
    Arguments = $"/c {command}",
    UseShellExecute = false,
    CreateNoWindow = false,
    ErrorDialog = false,
    RedirectStandardError = false,
    RedirectStandardOutput = false,
    WindowStyle = ProcessWindowStyle.Normal
};
Process.Start(start);

What's happening is that my console application (MyExecutable.exe) is executed, yet I do not see the Command window for it. It's launched in the background despite CreateNoWindow set to false and WindowStyle set to ProcessWindowStyle.Normal.

0 Answers
Related