XYZ
XYZ

Reputation: 299

Unable to create folder from batch file run by C#

I am having a batch file, which when runs independently creates a folder. But when I run the same batch file using C# code, I don't get anything. I am not sure how to debug this.

I checked the path of the batch file it's right.

C# code

string startupPath = System.IO.Directory.GetCurrentDirectory();
string bat = startupPath+@"\batch.bat";
var psi = new ProcessStartInfo();
psi.FileName = @"cmd.exe";
psi.Verb = "runas"; 
psi.Arguments = "/C " + bat;
Process.Start(psi);

Batch file

@ECHO OFF
ECHO.


cd C:\Users\rftx47\Documents\Visual Studio 2017\Projects\CertiHelper\CertiHelper
mkdir folderA

ECHO.
PAUSE
CLS
EXIT

What am I missing?

Upvotes: 1

Views: 422

Answers (1)

IamBatman
IamBatman

Reputation: 1015

It's doing exactly as it's supposed to, it's running the command console and then closing it. Exactly how you wrote it. I refactored it for you below.

            string startupPath = System.IO.Directory.GetCurrentDirectory();
            string bat = startupPath+@"\batch.bat";;
            var psi = new ProcessStartInfo();
            psi.FileName = @bat;  //this is where you need to put the file name.
            psi.Verb = "runas";
            psi.Arguments = "/c ";
            psi.UseShellExecute = true;  //this is where you start cmd
            Process.Start(psi);

Upvotes: 1

Related Questions