Execute batch file from C#

Costas

Administrator
Staff member
The WorkingDirectory property behaves differently when UseShellExecute is true than when UseShellExecute is false.

When UseShellExecute is true, the WorkingDirectory property specifies the location of the executable. If WorkingDirectory is an empty string, the current directory is understood to contain the executable.

When UseShellExecute is false, the WorkingDirectory property is not used to find the executable. Instead, its value applies to the process that is started and only has meaning within the context of the new process.

src - https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.processstartinfo.workingdirectory




**1**

The switch /k tells Command Prompt to issue the command that follows, and then stay open so that you can view results or type followup commands.
You can also use the /c switch (instead /k) if you want the Command Prompt window to close after issuing the command.
src - https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/cmd

JavaScript:
string args = string.Format("/k \"cd /d {0} && {1}\"", s.Path, s.Filename + " " + userChoice);
RunBatch(s.Filename, args, s.Path);
	
public static bool RunBatch(string cmd, string cmdArgs, string workingDir)
{
	//https://stackoverflow.com/questions/5519328/executing-batch-file-in-c-sharp
	//https://stackoverflow.com/questions/48321639/is-not-recognized-as-an-internal-or-external-command-operable-program-or-bat

	//MessageBox.Show("workingDir:" + workingDir + "\r\n\r\nFilename:" + cmd + "\r\n\r\nArguments:" + cmdArgs);
	ProcessStartInfo startInfo = new ProcessStartInfo()
	{
		UseShellExecute = false,
		WorkingDirectory = workingDir,
		FileName = "cmd.exe",
		Arguments = cmdArgs
	};

	Process.Start(startInfo);

	return true;
}


**2**

JavaScript:
//similar when UseShellExecute = true
[DllImport("Shell32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr ShellExecute(IntPtr hwnd, string lpOperation, string lpFile, string lpParameters, string lpDirectory, int nShowCmd);

ShellExecute(IntPtr.Zero, "open", @"C:\moon\test.bat", "-compress", @"C:\moon\", 1);


**3**

JavaScript:
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, string lParam);

[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern bool SetWindowText(IntPtr hWnd, string txt);

Process[] p = Process.GetProcesses().Where(p2 => p2.ProcessName.Equals( "cmd")).ToArray();

if (p.Count()>0)
{
    General.Mes("Currently " + p.Count() + " CMD window(s) exist, please close existing and retry.");
    return;
}

ProcessStartInfo startInfo2 = new ProcessStartInfo();
startInfo2.FileName = "cmd.exe";
startInfo2.Arguments = "/k";
Process process = Process.Start(startInfo2);

System.Threading.Thread.Sleep(1000);

//set dosbox caption
SetWindowText(process.MainWindowHandle, Application.ProductName + " CMD");

//send command to dosbox
//https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/sendkeys-statement
SendKeys.Send("pipiscrew{ENTER}");
 
Top