0

명령 프롬프트에서이 명령을 사용하면 완벽하게 올바르게 실행되는 배치 파일이 있습니다.C# 콘솔 응용 프로그램은 배치 파일을 실행할 수 없지만 배치 파일은 명령 프롬프트에서 제대로 작동합니다.

C:\app> C:\app\Process.bat C:\app\files\uploads c:\app\files file2 <- WORKS 

입력 매개 변수가 3 개뿐입니다. 나는 C에서 배치 파일을 실행하면

C:\app\files\uploads : the folder location of input files 
c:\app\files   : the output folder 
file2    : output file name 

는 : 응용 프로그램 폴더 \ 난 비주얼 스튜디오 디버그 모드 또는 클릭에서 작업 하지만 실행을 예약됩니다 콘솔 응용 프로그램에서 프로세스를 자동화하려는 출력 파일 참조 exe 파일은 아무것도하지 않습니다. 어떤 종류의 예외도 발생하지 않습니다.

무엇이 잘못 될 수 있습니까? 내가 잘못하고있는 권한이나 다른 것?

첫 번째 문제는 당신이하지 배치 파일을 cmd.exe를 실행해야 할 것입니다 :

는 C# 코드 여기

static void Main(string[] args) 
     { 
      RunBatchFile(@"C:\app\Process.bat", @"C:\app\files\uploads c:\app\files 123456"); 
     } 

public static string RunBatchFile(string fullPathToBatch, string args) 
     {    
      using (var proc = new Process 
      { 
       StartInfo = 
       { 
        Arguments = args,      
        FileName = fullPathToBatch, 

        UseShellExecute = false, 
        CreateNoWindow = true, 
        RedirectStandardOutput = false, 
        RedirectStandardError = false 
       } 
      }) 
      { 
       try 
       { 
        proc.Start(); 
       } 
       catch (Win32Exception e) 
       { 
        if (e.NativeErrorCode == 2) 
        { 
         return "File not found exception"; 
        } 
        else if (e.NativeErrorCode == 5) 
        { 
         return "Access Denied Exception"; 
        } 
       } 
      } 

      return "OK"; 
     } 

답변

1

이 문제입니다.
둘째, 실행 중이지만 프로세스가 완료 될 때까지 기다려야합니다. 앱이 부모 프로세스이며 자식 프로세스가 완료되지 않을 때까지 기다리지 않으므로 여기

var process = Process.Start(...); 
process.WaitForExit(); 

당신이 원하는 무엇인가 :

당신은 WaitForExit()를 실행해야

static void ExecuteCommand(string command) 
{ 
    int exitCode; 
    ProcessStartInfo processInfo; 
    Process process; 

    processInfo = new ProcessStartInfo("cmd.exe", "/c " + command); 
    processInfo.CreateNoWindow = true; 
    processInfo.UseShellExecute = false; 
    process = Process.Start(processInfo); 
    process.WaitForExit(); 
    exitCode = process.ExitCode; 
    process.Close(); 
} 

을 배치 파일을 실행하려면이) (주에서 수행

ExecuteCommand("C:\app\Process.bat C:\app\files\uploads c:\app\files file2"); 
+0

C# 코드에서 xyz.bat 파일을 실행할 수 없다는 말입니까? 이상한 소리가 난 – kheya

+0

아니, 배치 파일은 cmd.exe의 과정 내에서 실행 –

+0

배치 파일이 조금 복잡합니다. 큰 복잡한 명령을 직접 전달하는 대신 이름으로 호출하는 솔루션을 찾아야합니다. – kheya

관련 문제