I am designing a program in Windows Forms C# and I have the following problem:
The program has a button that, when clicked, executes a button .bat
that is in a location on the PC.
I have tried executing .exe
in this way and it works perfectly, but in the case of .bat
, clicking the button brings up the console window .bat
and quickly closes
the .bat
standalone works fine, running it with double click.
Button code:
private void button1_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start(@"E:\programas pcs\PROGRAMAS\instaladorx64.bat");
}
lines of the .bat *
@echo off
color 1f
Title Instalandor nombre
echo
echo
echo --------------------------------- ------------------------------------------
echo.
echo Instalando NETFRAMEWORK
echo.
dotnetfx45_full_x86_x64 /passive /norestart
echo.
echo.
cls
echo --------------------------------- -------------------------------------------
echo.
echo Instalando KM-Spico
echo.
sc stop WinDefend
Activador\Activador\KMSpico_setup.exe /SILENT
echo.
echo.
cls
It's a guess since you don't give many clues, but most likely the problem is simply that the .bat doesn't find the executables it tries to run.
You must add the property
WorkingDirectory
pointing to the folder where these executables are. Use the method that @marc gave you and add the line where theWorkingDirectory
(processInfo.WorkingDirectory = Path.GetDirectoryName(command);
) is added:The safest way to execute files
.bat
is to execute the windows console and pass the execution command of that file.bat
, an example of the code would be the following function, passing the file path as a parameter.bat
:You should call this function like this in your code: