2017-06-23 22 views
1

我有一个批处理文件,它在独立运行时创建一个文件夹。但是当我使用C#代码运行相同的批处理文件时,我什么也没得到。 我不知道如何调试。无法从C#运行的批处理文件创建文件夹

我检查了批处理文件的路径是正确的。

C#代码

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

批处理文件

@ECHO OFF 
ECHO. 


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

ECHO. 
PAUSE 
CLS 
EXIT 

我缺少什么?

+2

为什么不'''Process.Start(bat);''?或者''psi.FileName = bat;'' –

+0

我认为在这种情况下,命令提示符不会以管理员模式运行。我需要管理员模式。 – XYZ

+0

批处理文件的内容在哪里? 'startupPath'在哪里? –

回答

1

它完全按照它应该做的那样,它运行命令控制台然后关闭它。具体是怎么写的。我在下面为你重构它。

  string startupPath = System.IO.Directory.GetCurrentDirectory(); 
      string bat = [email protected]"\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);