2011-09-28 2 views
0

C# 코드 내에서 실행 파일 (installPrint.exe)을 시작해야합니다. 이 목적을 위해 System.Diagnostics.Process 클래스를 사용했습니다. exe 파일은 프린터 드라이버를 설치하고 여러 파일을 다른 디렉토리에 복사합니다. 명령 줄에서 exe를 실행할 수 있으며 모든 것이 잘 작동합니다. 하지만 C# 응용 프로그램에서 Process 클래스로 파일을 실행하면 프린터 드라이버가 설치되지 않습니다.프린터 드라이버를 설치하는 C#의 프로세스를 어떻게 실행합니까?

Windows XP SP2 x86 컴퓨터에서 관리 사용자로 C# 응용 프로그램을 시작합니다. 내 실행 파일이 C# 응용 프로그램의 컨텍스트에서 작동하지 않는 이유는 무엇입니까? 내가 할 수있는 가능성은 무엇입니까?

ProcessStartInfo startInfo = new ProcessStartInfo(); 
     startInfo.Arguments = "-i \"My Printer\" -dir . -port myPort -spooler"; 
     startInfo.CreateNoWindow = true; 
     startInfo.FileName = @"C:\Printer\install.exe"; 
     startInfo.RedirectStandardOutput = true; 
     startInfo.RedirectStandardError = true; 
     startInfo.UseShellExecute = false; 
     //startInfo.Verb = "runas"; 
     startInfo.WindowStyle = ProcessWindowStyle.Hidden; 
     startInfo.WorkingDirectory = @"C:\Printer\"; 
     session.Log("Working Directory: " + startInfo.WorkingDirectory); 

     session.Log("Executing " + startInfo.FileName); 
     try 
     { 
      Process process = new Process(); 
      //process.EnableRaisingEvents = false; 
      process.StartInfo = startInfo; 
      process.Start(); 

      session.Log("installer.exe started"); 
      StreamReader outReader = process.StandardOutput; 
      StreamReader errReader = process.StandardError; 
      process.WaitForExit(); 

      //session.Log(outReader.ReadToEnd()); 

      //session.Log(errReader.ReadToEnd()); 

      session.Log("RETURN CODE: " + process.ExitCode); 

     } 
     catch (Exception ex) 
     { 
      session.Log("An error occurred during printer installation."); 
      session.Log(ex.ToString()); 
     } 
+0

예외가 있습니까? 오류가 있습니까? – Saint

+0

프린터 드라이버를 추가 할 수 없다는 정보 만받습니다. 또한 설치 프로그램은 로컬 프린터 포트를 만듭니다. 이것은 잘 작동하지만 프린터를 추가 할 때는 실패합니다. – CubaLibre

+1

실패했습니다. 나는 setCreateNoWindow = false를 사용하고 쉘을 사용하고 이제는 작동한다. – CubaLibre

답변

2

난 당신이 모든 액세스 권한으로 실행하기 위해 새로 생성 된 프로세스에 대한 고도를 요청해야, 당신은 그런 다음 Windows Vista 또는 7에서 프로그램을 실행하고, 그것을 가지고. 자세한 내용은 그 질문에 봐 : Request Windows Vista UAC elevation if path is protected? Windows 7 and Vista UAC - Programmatically requesting elevation in C#

좋아, 난 당신이 승리 XP를 사용하고 있는지, 지금 참조하십시오. 그런 다음 프로세스를 시작할 때 프로세스의 일부 설정이 원인 일 수 있습니다. ShellExecute로 프로세스를 시작하십시오.이 방법은 사용자가 시작하는 방식에 가장 가깝습니다.

var p = new System.Diagnostics.Process(); 
p.StartInfo = new System.Diagnostics.ProcessStartInfo { FileName = "yourfile.exe", UseShellExecute = true }; 
p.Start(); 
+1

Windows XP SP2를 사용 중입니다. – CubaLibre

+0

답변 본문의 업데이트를 참조하십시오. –

+0

나는 useShellExecute를 시도했지만 효과가 없다. 나는 사용자 또는 다른 프로세스에 의한 프로세스 시작 사이의 차이점을 이해하지 못한다. 실행 프로세스의 하위 프로세스이기 때문일 수 있습니까? – CubaLibre

0

내 프로젝트의 많은 지역에서이 클래스를 사용 :

public class ExecutableLauncher 
{ 
    private string _pathExe; 

    public ExecutableLauncher(string pathExe) 
    { 
     _pathExe = pathExe; 
    } 
    public bool StartProcessAndWaitEnd(string argoment, bool useShellExecute) 
    { 
     try 
     { 
      Process currentProcess = new Process(); 

      currentProcess.EnableRaisingEvents = false; 

      currentProcess.StartInfo.UseShellExecute = useShellExecute; 

      currentProcess.StartInfo.FileName = _pathExe; 

      // Es.: currentProcess.StartInfo.Arguments="http://www.microsoft.com"; 
      currentProcess.StartInfo.Arguments = argoment; 

      currentProcess.Start(); 
      currentProcess.WaitForExit(); 
      currentProcess.Close(); 

      return true; 
     } 
     catch (Exception currentException) 
     { 
      throw currentException; 
     } 
    } 
} 

내가 당신의 질문에 대답 한 희망 다음은 샘플입니다.

M.

+1

useShellExecute를 사용하거나 사용하지 않고 여러 옵션을 시도한 다음 startInfo.Verb = "runas"를 시도했습니다. 그러나 아무 것도 효과가 없었습니다. 나는 그물을 통해 가능성을 찾았습니다. 프로세스가 다른 프로세스에서 실행되는 동안 시스템 설정에 액세스하는 것이 문제인 것 같습니다. – CubaLibre

+0

아마도 startInfo.Arguments = "-i \"My Printer \ "-dir .port myPort -spooler"내에 문제가있을 수 있습니다. 일부 매개 변수를 확인하고 제거하십시오. 너 확실해? " 국장님? –

+1

문제를 발견했습니다. 메인 포스트의 댓글을 참조하십시오. – CubaLibre

관련 문제