run a .bat from C # without displaying the CMD window

7

I'm creating a .exe to run a .jar, it works, the only problem is that it shows a pop-up window ( CMD ) for a few milliseconds, is there any way to make this not happen?

Here is my code to run the .bat :

using System;

namespace Launch
{
    class MainClass
    {
        public static void Main(string[] args)

        {
            System.Diagnostics.Process.Start("start.bat");
        }
    }
}

this is my .bat :

@echo off
cd "C:\Program Files (x86)\START"
"C:\Program Files (x86)\START\jre8\bin\javaw.exe" -jar -XX:+UseConcMarkSweepGC -Xmx1024M -Xms1024M START.jar

Another way I tried was to do everything from C# (but I got the same result):

using System.Diagnostics;

namespace Launch
{
    class MainClass
    {
        public static void Main(string[] args)

        {           

            ProcessStartInfo psi = new ProcessStartInfo();
            psi.Arguments = "-jar -XX:+UseConcMarkSweepGC -Xmx1024M -Xms1024M START.jar";
            psi.CreateNoWindow = true;
            psi.WindowStyle = ProcessWindowStyle.Hidden;
            psi.FileName = "jre8\bin\javaw.exe";
            Process.Start(psi);


        }
    }
}
    
asked by Angel Montes de Oca 08.11.2017 в 19:25
source

2 answers

5

You need to define the use of a Shell ( UseShellExecute ) as false, here is your code along with the line you need:

 ProcessStartInfo psi = new ProcessStartInfo();
 psi.UseShellExecute = false;  
 psi.Arguments = "-jar -XX:+UseConcMarkSweepGC -Xmx1024M -Xms1024M START.jar";
 psi.CreateNoWindow = true;
 psi.WindowStyle = ProcessWindowStyle.Hidden;
 psi.FileName = "jre8\bin\javaw.exe";
 Process.Start(psi);
    
answered by 08.11.2017 / 19:31
source
1

you can:

Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & "C:pepe.bat" & Chr(34), 0
Set WshShell = Nothing

or

set objshell = createobject ("wscript.shell")
objshell.run "nombredetuarchivo.bat" , vbhide

or

Dim WinScriptHost Set WinScriptHost = CreateObject("WScript.Shell") WinScriptHost.Run Chr(34) & "C:\Scheduled Jobs\mybat.bat" & Chr(34), 0 Set WinScriptHost = Nothing
    
answered by 08.11.2017 в 19:38