1

I have to run a .bat file from c#... I use this method.

 file = "C:\\Diego\\PublishCore\\Startup_service.bat";
                ProcessStartInfo psi = new ProcessStartInfo();
                psi.CreateNoWindow = true; 
                psi.FileName = file;
                psi.UseShellExecute = true;
                psi.Verb = "runas";
                Process.Start(psi);

.BAT is executed... but the action I ask to perfom it does not execute...

If my .bat says MKDir MyDir... Its creates a Directory called MyDIr with no problems.

But when my bat says dotnet myApp.dll, a cmd Windows opens and closes, but it does not start myApp aplication....

If a doublé-click my .bat is runs fine.

What I am missing? Why the aplication does not start?

Kiquenet
  • 14,494
  • 35
  • 148
  • 243
Diego
  • 2,238
  • 4
  • 31
  • 68
  • Duplicate : https://stackoverflow.com/questions/38321981/how-to-use-c-sharp-run-batch-file-as-administrator-to-install-windows-services#38322466 – Witzig Adrien Oct 08 '18 at 12:58
  • 1
    It has nothing to do my problem.. I am running as Administrador. My problem is that Windows opens a closes without executing it... Thanks – Diego Oct 08 '18 at 13:58
  • 1
    I would think it cannot find the location of your program because when you run a batch file as administrator, the working directory becomes SYSTEM32. – Squashman Oct 08 '18 at 14:26
  • We can help you better troubleshoot your .bat script if you post the code for it. – Wes Larson Oct 09 '18 at 05:26

1 Answers1

1

I solved it...

The problem was that, as my bat run the instruction dotnet myApp.dll. I set the path file where the file was, but it was executed in the location where the my Solution is, instead of running in the same directory where I have .bat file.

I have to set WorkingDirectory and Arguments

 C:\\Diego\\PublishCore\\Startup_InomCore.bat
 
 ProcessStartInfo psi = new ProcessStartInfo();
                psi.WorkingDirectory = "C:\\Diego\\PublishCore";
                //   psi.CreateNoWindow = true; 
                psi.FileName = @"cmd.exe";
                psi.Arguments = "/c start /wait " + "C:\\Diego\\PublishCore\\Startup_InomCore.bat";
                //     psi.UseShellExecute = true;
                psi.Verb = "runas"; 
                var process = Process.Start(psi);
Diego
  • 2,238
  • 4
  • 31
  • 68