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