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

2 回答 2

2

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

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

var p = new System.Diagnostics.Process();
p.StartInfo = new System.Diagnostics.ProcessStartInfo { FileName = "yourfile.exe", UseShellExecute = true };
p.Start();
于 2011-09-28T07:55:15.403 回答
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。

于 2011-09-28T14:58:07.147 回答