2011-09-28 167 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并使用shell执行,现在它工作。 – CubaLibre

回答

2

我把它,您在Windows Vista或7上运行您的程序然后,您有权要求提升为新创建的过程中具有完全访问权限运行。看看这些问题的详细信息: Request Windows Vista UAC elevation if path is protected? Windows 7 and Vista UAC - Programmatically requesting elevation in C#

好吧,我现在看到,您使用的是Win XP的。那么可能是因为Process启动时的某些设置。尝试开始你作为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”中有问题。尝试检查并删除一些参数。你确定“。” DIR? –

+1

我发现了这个问题。请参阅主文章中的评论。 – CubaLibre

相关问题