2014-09-29 2 views
0

.net 코드에서 실행중인 공유 위치에 배치 파일이 있지만 배치 파일이 실제로 실행되지 않습니다. 배치 파일의 전체 코드가 작동하지 않습니다. 단지 부분 코드 만 작동합니다. 나는 .net 코드 실행이 완료되고 배치 파일의 실행을 중단한다고 가정합니다. 다음은 내 .NET 코드배치 파일이 C# 코드에서 실행되지 않음

private static void ExecuteCommand(string command, string arguments) 
    { 
     try 
     { 
      Console.WriteLine("Code execution starts"); 
      Console.WriteLine("command: " + command); 
      Console.WriteLine("arguments: " + arguments); 
      var process = new Process(); 

      process.StartInfo.FileName = command; 
      if (!string.IsNullOrEmpty(arguments)) 
      { 
       process.StartInfo.Arguments = arguments; 
      } 

      process.StartInfo.CreateNoWindow = true; 
      process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; 
      //process.StartInfo.CreateNoWindow = false; 
      //process.StartInfo.WindowStyle = ProcessWindowStyle.Normal; 
      process.StartInfo.UseShellExecute = true; 
      process.EnableRaisingEvents = true; 
      //process.StartInfo.RedirectStandardError = true; 
      //process.StartInfo.RedirectStandardOutput = true; 
      string stdError; 
      // process.Start(); 
      Thread.Sleep(60000); 
      ThreadPool.QueueUserWorkItem(delegate 
      { 
       process.Start(); 
       // process.WaitForExit(); 
      }); 
      //Thread.Sleep(60000); 

      //ThreadStart ths = new ThreadStart(delegate() 
      //{ 
      // process.Start(); 
      // //process.BeginOutputReadLine(); 
      // //stdError = process.StandardError.ReadToEnd(); 
      //}); 

      //process.Start(); 

      //process.BeginOutputReadLine(); 
      //stdError = process.StandardError.ReadToEnd(); 

      Console.WriteLine("Code execution ends"); 
     } 
     catch (Exception e) 
     { 
      Console.WriteLine("Code execution error: "+e.Message +"||"+e.InnerException.ToString()); 

     } 
    } 

요구 사항이 배치 파일의 실행을 시작하고 NTO 배치 파일 실행의 출력 또는 마무리를 기다릴 않습니다이다. 내가 명령 프롬프트 또는 Windows 탐색기에서 배치 파일을 실행하면 정상적으로 작동합니다. 배치 파일이 없거나 권한 문제가있을 때 예외를 처리해야하기 때문에 .net 코드에서 cmd.exe를 사용할 수 없습니다.

+0

'process.StartInfo'에서'WorkingDirectory' 설정을 시도 했습니까? 배치가 중단 된 배치 파일의 실행을 확인합니다. 어쩌면 어떤 오류가있을 수 있습니다. – JeffRSon

답변

0

수면을 사용하지 않고 WaitForExit 메서드를 사용할 수 있다고 생각합니다. 충분 해.

그것에 대해

더 많은 정보 : http://msdn.microsoft.com/en-us/library/ty0d8k56.aspx

가 당신은 프로세스가 시작되기 전에 생성과 종료 배치에 의해 삭제 된 파일을 사용하여 같은 해결 방법을 사용할 수 있습니다, 그리고 그것을 확인있어, 작동하지 않는 경우 프로세스가 완료되었는지 확인하는 방법으로 주기적으로 FileSystemWatcher (http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher(v=vs.110).aspx)을 입력하십시오.

관련 문제