2017-10-27 1 views
-1

콘솔을 표시하는 C# 콘솔 앱에서 하위 프로세스를 만들려고합니다. 나는 다음과 같은 시도했지만 아무 창이 나타나지 않았다.하위 프로세스 창이 나타나지 않습니다.

 ProcessStartInfo = new ProcessStartInfo(name) 
     { 
      UseShellExecute = false, 
      RedirectStandardError = true, 
      RedirectStandardOutput = true, 
      WindowStyle = ProcessWindowStyle.Maximized, 
      CreateNoWindow = false, 
      ErrorDialog = false 
     }; 

     if (args != null) 
     { 
      ProcessStartInfo.Arguments = args; 
     } 

     if (workingDirectory != null) 
     { 
      ProcessStartInfo.WorkingDirectory = workingDirectory; 
     } 

     Process = new Process {EnableRaisingEvents = true, StartInfo = ProcessStartInfo}; 
     Process.Start(); 
+0

'name','args' 및'workingDirectory'의 값은 무엇입니까? – mjwills

+0

이름은이 토론에 대한 하위 콘솔 앱 (MyApp.exe)의 이름이며 작업중인 workingDirectory가 모두 null입니다. –

답변

0

부모의 콘솔에서 자식 프로세스를 실행하는 올바른 방법은 설정에 ProcessStartInfo 클래스의 UseShellExecute 속성입니다. time 명령을 실행하는 예제를 생각해 보겠습니다. 왜 시간? 표준 입력에서 읽으므로. 이렇게하면 어떤 콘솔을 사용하는지 알 수 있습니다.

public class Program 
{ 
    public static void Main(string[] args) 
    { 
     var processInfo = new ProcessStartInfo 
     { 
      FileName = "cmd.exe", 
      Arguments = "/c time" 
     }; 

     Console.WriteLine("Starting child process..."); 
     using (var process = Process.Start(processInfo)) 
     { 
      process.WaitForExit(); 
     } 
    } 
} 

우리는 true입니다 UseShellExecute의 기본 값을 떠났다. 이는 쉘이 하위 프로세스에 사용될 것임을 의미합니다. 셸 사용은 새 콘솔이 만들어 짐을 의미합니다.

UseShellExecute의 값을 false으로 바꿔 봅시다.

public class Program 
{ 
    public static void Main(string[] args) 
    { 
     var processInfo = new ProcessStartInfo 
     { 
      UseShellExecute = false, // change value to false 
      FileName = "cmd.exe", 
      Arguments = "/c time" 
     }; 

     Console.WriteLine("Starting child process..."); 
     using (var process = Process.Start(processInfo)) 
     { 
      process.WaitForExit(); 
     } 
    } 
} 
+0

출력을 리디렉션을 제거하기 위해 몇 가지 변경 사항이 있으므로 창이 나타납니다. 도움 주셔서 감사합니다. –

관련 문제