C
C#4mo 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
jborean4mo ago
try running cmd.exe as the FileName then arguments being /k path.bat
[SK] Cody
[SK] Cody4mo ago
not a bad idea, ill give that a try thank you
i like chatgpt
i like chatgpt4mo ago
@[SK] Cody Are you using the default of CreateNoWindow=false and UseShellExecute=false?
[SK] Cody
[SK] Cody4mo ago
i have not set those so i assume yes if they are false by default no luck
i like chatgpt
i like chatgpt4mo ago
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] Cody4mo 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
}
i like chatgpt
i like chatgpt4mo ago
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
jborean4mo 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] Cody4mo 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