Help, I can not run external .bat from my C # windows forms application

2

I'm designing a program in Windows Forms C # and I have the following problem:

The program has a button that when clicked, it executes a .bat that is in a location of the pc.

I've tried running .exe in this way and it's perfect but in the case of the .bat , when you click on the button the console window of .bat comes out and it quickly closes

the .bat independently works fine, executing it with double click.

Button code:

  private void button1_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start(@"E:\programas pcs\PROGRAMAS\instaladorx64.bat");

        } 

.bat lines *

@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
    
asked by HOLDTHEDOOR 07.06.2018 в 16:08
source

2 answers

7

It's a guess since you do not give many clues, but most likely the problem is simply that the .bat does not find the executables it tries to run.

You must add the WorkingDirectory property pointing to the folder where those executables are. Use the method that put @marc and add the line where the WorkingDirectory ( processInfo.WorkingDirectory = Path.GetDirectoryName(command); ) is added:

static void ExecuteCommand(string command)
{
    int exitCode;
    ProcessStartInfo processInfo;
    Process process;

    processInfo = new ProcessStartInfo("cmd.exe", "/c " + command);
    processInfo.CreateNoWindow = true; //Esta propiedad oculta la consola
    processInfo.UseShellExecute = false;
    processInfo.WorkingDirectory = Path.GetDirectoryName(command);

    process = Process.Start(processInfo);
    process.WaitForExit();

    exitCode = process.ExitCode; //Si tu bat tiene exit code lo obtendrá aquí

    process.Close();
}
    
answered by 07.06.2018 / 17:24
source
5

The safest way to run .bat files is to run the windows console and pass the execution command of that file .bat , an example of the code would be the following function, passing as a parameter the path of the file .bat :

static void ExecuteCommand(string command)
{
    int exitCode;
    ProcessStartInfo processInfo;
    Process process;

    processInfo = new ProcessStartInfo("cmd.exe", "/c " + command);
    processInfo.CreateNoWindow = true; //Esta propiedad oculta la consola
    processInfo.UseShellExecute = false;

    process = Process.Start(processInfo);
    process.WaitForExit();

    exitCode = process.ExitCode; //Si tu bat tiene exit code lo obtendrá aquí

    process.Close();
}

You should call this function in the following way in your code:

ExecuteCommand(@"E:\programas pcs\PROGRAMAS\instaladorx64.bat");
    
answered by 07.06.2018 в 16:18