6 Replies - 3549 Views - Last Post: 09 August 2010 - 04:56 AM Rate Topic: -----

#1 cuda66  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 04-August 10

problem launching batch file from C# application

Posted 08 August 2010 - 09:24 AM

I have another problem.
This time with a batch application launched from a windows forms (button click event)

The code to launch is this:
ProcessStartInfo ProcessInfo;
                Process Process;

                ProcessInfo = new ProcessStartInfo("cmd.exe", "/C " + "C:\\test\\test.bat");
                ProcessInfo.CreateNoWindow = false;
                ProcessInfo.UseShellExecute = true;
                Process = Process.Start(ProcessInfo);


                Process.Close();



The problem is that if I execute this batch from C# I receive the black window for a brief time and then nothing (no process launched) - the bat file should start a server applications that you can see in task manager.
If I execute the batch file directly from windows then everything works and the server application starts...

Is This A Good Question/Topic? 0
  • +

Replies To: problem launching batch file from C# application

#2 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 809
  • View blog
  • Posts: 1,949
  • Joined: 04-October 09

Re: problem launching batch file from C# application

Posted 08 August 2010 - 10:31 AM

View Postcuda66, on 08 August 2010 - 07:24 AM, said:

Process = Process.Start(ProcessInfo);
Process.Close();

You start the process and immediately close it. Put a WaitForExit() between those.

I'd also change the name of my variable from Process to something that wasn't the name of an object.

This post has been edited by Momerath: 08 August 2010 - 10:32 AM

Was This Post Helpful? 0
  • +
  • -

#3 cuda66  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 04-August 10

Re: problem launching batch file from C# application

Posted 08 August 2010 - 10:57 AM

Thanks for the suggestion.
I forgot about this...
I put waitforexit() but it's a no go...
It still doesn't work....

The batch file receive some parameters from the C# application (2 parameters) and then in the same button_click event, after passing this two parameters I added the code to launch the batch file...
But even with waitforexit() it's the same black window flickering for short time and nothing is launched...
The parameters are passed correctly because if I launch the batch file directly from windows after passing them from C# the batch file launch the server application without any intervention correctly...
Was This Post Helpful? 0
  • +
  • -

#4 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 809
  • View blog
  • Posts: 1,949
  • Joined: 04-October 09

Re: problem launching batch file from C# application

Posted 08 August 2010 - 12:59 PM

Without seeing the code I can't help you.
Was This Post Helpful? 0
  • +
  • -

#5 cuda66  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 04-August 10

Re: problem launching batch file from C# application

Posted 08 August 2010 - 01:29 PM

O.K.

This is the code for the batch file:
@echo off


 
echo: 
echo: 

echo:       
   
:choice

set choice=

echo:      
         

echo:      
:choice2

set choice2=


set /a starting_port = 13200
set /a port_add = %starting_port%
set /a port_end = %starting_port% + %choice%
echo:            
echo: you open %choice% connections and Add Port Firewall %starting_port% - %port_end%
echo: 
echo: Add Port Firewall : %starting_port% - %port_end%
echo: wait ...
echo: 
echo: 
:netsh
netsh firewall add portopening ALL %port_add% ourfirmPort%port_add% 2>NUL>NUL
set /a port_add += 1
if %port_add% == %port_end% goto red
goto netsh

:red
echo: 

set /a max_connections = %choice%
set /a start_delay = %choice2%
set /a starting_port = 13200
set /a port_add = %starting_port%
set /a port_end = %starting_port% + %max_connections%
set /a sv_add = 0

:start

FOR /F "tokens=2 delims=:" %%A IN ('ipconfig ^| find /I /C "RDS"') DO SET RDS=%%A
IF "%RDS%"==" 1" (
    FOR /F "tokens=2 delims=:" %%A IN ('ipconfig ^| find /I "IP"') DO SET IPARG=+ip^ %%A
)
set /a server_curr = %sv_add% + 1
echo: Starting  navisionserver %server_curr% on port %port_add%
echo: wait %start_delay% sec ....
echo: 
start /b /min /low navision.exe -console - %IPARG% -secure -pingboost 3 +sys_ticrate 10000 -heapsize 15000 +port %port_add% 
set /a port_add += 1
set /a sv_add +=1
if %port_add% == %port_end% goto net
ping 1.1.1.1 -n %start_delay% -l 1 -w 2500 >NUL
goto start
:net

ping 1.1.1.1 -n %start_delay% -l 1 -w 2500 >NUL
CLS
GOTO END



And this is the C# code for the button that passes the arguments and SHOULD start the batch file:

 {


                string path = @"C:\test\test.bat";

                string[] fileStrings = File.ReadAllLines(path);
                for (int i = 0; i < fileStrings.Length; i++)
                {
                    if (fileStrings[i].Contains("set choice="))
                    {
                        fileStrings[i] = "set choice=" + textBox3.Text;
                        break;
                    }
                }
                for (int i = 0; i < fileStrings.Length; i++)
                {
                    if (fileStrings[i].Contains("set choice2="))
                    {
                        fileStrings[i] = "set choice2=" + textBox4.Text;
                        break;
                    }
                }

                File.WriteAllLines(path, fileStrings);


            }

            {
                ProcessStartInfo ProcessInfo;
                Process Process;

                ProcessInfo = new ProcessStartInfo("cmd.exe", "/C " + "C:\test\test.bat");
                ProcessInfo.CreateNoWindow = false;
                ProcessInfo.UseShellExecute = true;
                Process = Process.Start(ProcessInfo);
                Process.WaitForExit();

                Process.Close();


            }


Was This Post Helpful? 0
  • +
  • -

#6 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 809
  • View blog
  • Posts: 1,949
  • Joined: 04-October 09

Re: problem launching batch file from C# application

Posted 08 August 2010 - 02:38 PM

View Postcuda66, on 08 August 2010 - 11:29 AM, said:

ProcessInfo = new ProcessStartInfo("cmd.exe", "/C " + "C:\test\test.bat");

You either need to put an @ sign in front of the last string there, or double up on the \ symbols, i.e. ... "/C " + "C:\\test\\test.bat");
Also, why don't you just make that one string, rather than adding two together?
Was This Post Helpful? 0
  • +
  • -

#7 cuda66  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 04-August 10

Re: problem launching batch file from C# application

Posted 09 August 2010 - 04:56 AM

I finally get it working...

string targetDir;
                    targetDir = string.Format(@"C:\test");
                    p = new Process();
                    p.StartInfo.WorkingDirectory = targetDir;
                    p.StartInfo.FileName = "test.bat";
                    
                    
                    p.Start();
                    p.StartInfo.CreateNoWindow = true;
                    p.WaitForExit();
                    p.Close();


Now I have another problem: Is there a way to hide the batch file execution window???
With CreateNowindow set to true the "ugly" black window is still showing...

This post has been edited by cuda66: 09 August 2010 - 08:49 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1