【问题标题】:How do i execute a process in C# that installs a printer driver?如何在 C# 中执行安装打印机驱动程序的进程?
【发布时间】:2011-09-28 07:51:37
【问题描述】:

我必须在我的 C# 代码中启动一个可执行文件 (installPrint.exe)。为此,我使用了 System.Diagnostics.Process 类。 exe 文件安装打印机驱动程序并将多个文件复制到不同的目录中。我可以从命令行执行exe,一切正常。但是如果我从我的 C# 应用程序中使用 Process 类执行文件,则不会安装打印机驱动程序。

我以管理员用户身份在 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());
        }

【问题讨论】:

  • 我只得到无法添加打印机驱动程序的信息。安装程序还会创建一个本地打印机端口。这工作正常,但在添加打印机时它失败了。
  • 我发现了我的失败。我 setCreateNoWindow = false 并使用了 shell 执行,现在它可以工作了。

标签: c# .net visual-studio-2010 printers


【解决方案1】:

我认为,您正在 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();

【讨论】:

  • 我已经尝试过 useShellExecute 并没有效果。我不明白由用户或另一个进程启动一个进程有什么区别?难道是因为它是执行进程的子进程?
  • 可能问题出在驱动包本身?就像它期望从某个特定的地方启动一样,等等。也许您应该将工作目录设置为文件所在的同一目录?因为,默认情况下,如果您使用 Process.Start,AFAIK 工作目录将与您的主机进程相同。
  • 我正在使用正确的工作目录。我在主帖中添加了我的示例代码。
  • 对于 Windows 7,您的解决方案有效,但现在 Windows 两次要求我输入管理员密码。首先,当我启动安装程序时,没关系。第二次在安装过程中(管理员权限已授予安装程序),当 installer.msi 为打印机启动时。有没有办法避免二次授权?
【解决方案2】:

我在项目的很多部分都使用这个类:

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.

【讨论】:

  • 我尝试了几个选项,有和没有 useShellExecute,然后我尝试了 startInfo.Verb = "runas"。但没有任何效果。我在网上搜索了让它工作的可能性。从另一个进程执行进程时访问系统设置似乎有问题?
  • 可能 startInfo.Arguments = "-i \"My Printer\" -dir . -port myPort -spooler";尝试检查并删除一些参数。你确定“。”目录?
  • 我发现了问题。请参阅主帖中的评论。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-16
  • 2016-10-07
  • 1970-01-01
  • 2010-12-01
  • 1970-01-01
  • 2021-12-07
相关资源
最近更新 更多