C
C#•15mo ago
[SK] Cody

Execute batch file and keep cmd open

I've written my program to execute a batch file and pass in the needed arguments, however I'm having an issue of trying to figure out how to keep the cmd window open after the batch file has finished executing. I cannot modify the batch file. Originally the way I was performing my task was to add cmd /k at the start of a batch file that would then execute the batch file I was targeting and passing the arguments that way but I'm trying to automate the process more Currently it's just
c#
Process uatProcess = new Process();
uatProcess.StartInfo.FileName = uatPath; // Path to the bat file
uatProcess.StartInfo.Arguments = arguments; // Aruments being passed in
c#
Process uatProcess = new Process();
uatProcess.StartInfo.FileName = uatPath; // Path to the bat file
uatProcess.StartInfo.Arguments = arguments; // Aruments being passed in
9 Replies
jborean
jborean•15mo ago
try running cmd.exe as the FileName then arguments being /k path.bat
[SK] Cody
[SK] CodyOP•15mo ago
not a bad idea, ill give that a try thank you
🥄feeders help me learn faster !
@[SK] Cody Are you using the default of CreateNoWindow=false and UseShellExecute=false?
[SK] Cody
[SK] CodyOP•15mo ago
i have not set those so i assume yes if they are false by default no luck
🥄feeders help me learn faster !
With the following, the application shares the same console output with cmd.exe executing the batch. The application window still opens if you add Console.ReadKey().
ProcessStartInfo info = new()
{
FileName = @"cmd.exe",
Arguments = @"/c actions.bat",
CreateNoWindow = false, // default false (it is ignored when UseShellExecute=true)
UseShellExecute = false // default false
};
using var process = new Process { StartInfo = info };
process.Start();
Console.ReadKey();
ProcessStartInfo info = new()
{
FileName = @"cmd.exe",
Arguments = @"/c actions.bat",
CreateNoWindow = false, // default false (it is ignored when UseShellExecute=true)
UseShellExecute = false // default false
};
using var process = new Process { StartInfo = info };
process.Start();
Console.ReadKey();
[SK] Cody
[SK] CodyOP•15mo ago
no dice, heres a small snippet of the function (using WPF), confirmed the constructed string for the arguments is correct. not entirely sure what im missing
c#
private int ExecuteProcess(string uatPath, string arguments)
{
Process uatProcess = new Process();
uatProcess.StartInfo.FileName = "cmd.exe";
uatProcess.StartInfo.Arguments = "/c " + uatPath + " " + arguments;
uatProcess.StartInfo.CreateNoWindow = false;
uatProcess.StartInfo.UseShellExecute = false;

uatProcess.Start();
uatProcess.WaitForExit();
return uatProcess.ExitCode
}
c#
private int ExecuteProcess(string uatPath, string arguments)
{
Process uatProcess = new Process();
uatProcess.StartInfo.FileName = "cmd.exe";
uatProcess.StartInfo.Arguments = "/c " + uatPath + " " + arguments;
uatProcess.StartInfo.CreateNoWindow = false;
uatProcess.StartInfo.UseShellExecute = false;

uatProcess.Start();
uatProcess.WaitForExit();
return uatProcess.ExitCode
}
🥄feeders help me learn faster !
Do you want to display the Process' output on a WPF control such as a list box? Or let the output be shown on the cmd window? For the latter case:
private int ExecuteProcess(string uatPath, string arguments)
{
Process uatProcess = new Process();
uatProcess.StartInfo.FileName = "cmd.exe";
uatProcess.StartInfo.Arguments = $"/k {uatPath} {arguments}";
uatProcess.StartInfo.UseShellExecute = true;
uatProcess.Start();
uatProcess.WaitForExit();
return uatProcess.ExitCode
}
private int ExecuteProcess(string uatPath, string arguments)
{
Process uatProcess = new Process();
uatProcess.StartInfo.FileName = "cmd.exe";
uatProcess.StartInfo.Arguments = $"/k {uatPath} {arguments}";
uatProcess.StartInfo.UseShellExecute = true;
uatProcess.Start();
uatProcess.WaitForExit();
return uatProcess.ExitCode
}
jborean
jborean•15mo ago
if there is a space in the path you need to quote uatPath, otherwise using ArgumentList IIRC has dotnet do that for you
[SK] Cody
[SK] CodyOP•15mo ago
let it be shown in the CMD window, did the former and did not like the result to much, ill give that a shot

Did you find this page helpful?