如何在 C# 中执行安装打印机驱动程序的进程?

发布于 2024-12-06 22:00:55 字数 1689 浏览 0 评论 0原文

我必须在 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());
        }

I have to start an executable (installPrint.exe) within my C# code. For this purposes I used the System.Diagnostics.Process class. The exe file installs a printer driver and copy several files into different directories. I can execute the exe from command line and everything work fine. But if i execute the file with the Process class from my C# application, the printer driver will not be installed.

I start my C# application as a admin user on a Windows XP SP2 x86 machine. Why do my executable dont work in the context of my C# application? What possibilities do i have to get it work?

 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());
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清秋悲枫 2024-12-13 22:00:55

我认为,您正在 Windows Vista 或 7 上运行您的程序。然后,您必须请求提升新创建的进程才能以完全访问权限运行。查看这些问题以了解详细信息:
如果路径受保护,请求 Windows Vista UAC 提升?
Windows 7 和 Vista UAC - 在 C# 中以编程方式请求提升< /a>

好的,我现在明白了,您使用的是 Win XP。那么可能是因为启动Process时的一些设置导致的。尝试以 ShellExecute 方式启动进程,这样它将最接近用户的正常启动。
这是一个示例:

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

I take it, you are running your program on Windows Vista or 7. Then, you have to request elevation for your newly created process to run with full access rights. Look at those questions for details:
Request Windows Vista UAC elevation if path is protected?
Windows 7 and Vista UAC - Programmatically requesting elevation in C#

Ok, I see now, that you're using Win XP. Then it may be because of some settings of Process when you start it. Try to start you process as ShellExecute, this way it will be most close to normal starting by the user.
Here's a sample:

var p = new System.Diagnostics.Process();
p.StartInfo = new System.Diagnostics.ProcessStartInfo { FileName = "yourfile.exe", UseShellExecute = true };
p.Start();
岁月流歌 2024-12-13 22:00:55

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

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。

I use this class in many parts of my projects:

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;
        }
    }
}

I hope to have answered at your question.

M.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文