0

I'm trying to execute a batch file in C#, but although I have found multiple examples on the Internet it is not working for me.

My code:

ProcessStartInfo psi = new ProcessStartInfo(pathBat);
psi.Verb = "runas";
psi.UseShellExecute = false;
Process.Start(psi);

Content of .bat file:

MIL_detection_app.exe -monitor AEE86079 2>&1 | logger.exe
pause

MIL_detection_app.exe is in the same folder. If I try to run .bat by double click it works fine but when I try through C# code, a console show up and disappear in less than a second.

What am I doing wrong?

Sinatr
  • 20,892
  • 15
  • 90
  • 319
Neus
  • 17
  • 5
  • Probably there is an error or it finish the job and then it is closed. Do you want to [keep console window opened](https://stackoverflow.com/a/23921027/1997232)? – Sinatr Apr 12 '18 at 10:28
  • I have read all the posts related but the solutions haven't worked for me. – Neus Apr 12 '18 at 10:30
  • Are you running it from the right directory.. you supplied "runas" but no credentials. what is in pathBat? – BugFinder Apr 12 '18 at 10:31
  • Hi Sinatr, the .exe that is executed by .bat file, should open a console window (and it does not). I want to see this console window. I have tried also the command waitForExit() but without success. – Neus Apr 12 '18 at 10:32
  • pathBat is the folder where .bat is. In my case: C:\Users\nraga\Desktop\C#\PruebaLanzador\PruebaLanzador\bin\x64\Debug\Current\launch.bat – Neus Apr 12 '18 at 10:33
  • dont use runas! just run it with the processtart - and maybe set the start folder. – BugFinder Apr 12 '18 at 10:35
  • Could you explain why I should not use runas, please? I don't understand a lot, buy I thought it was for run as administrator. If I comment this line of code, the result is the same :( Thank you – Neus Apr 12 '18 at 10:39
  • What is your c# app is it an app your user runs, or a service? it sounds like a service or scheduled task.. runas usually is for running as different credentials which you then supply.. it just sounds like those exes are not pathed. – BugFinder Apr 12 '18 at 10:47
  • Voting to close are there are numerous discussions on SO around this, – Gerhard Apr 12 '18 at 11:00
  • My app is a launcher for another app. It is supposed to connect to a shared resource, download the last version and run it. Until now, this process was manual by double click the file launch.bat, but now, I need my app (which is a launcher) does it and executes launch.bat. I hope to have make clear myself, thanks. – Neus Apr 12 '18 at 11:06
  • Setting the start folder worked for me! Thank you very much! @BugFinder – Neus Apr 12 '18 at 13:41

0 Answers0