【发布时间】:2018-11-01 12:34:38
【问题描述】:
我有一个指令可以从 C# (WPF) 应用程序以编程方式安装打印机驱动程序。不得允许用户退出我的应用程序并从 Windows 安装。我已经设法通过手动使其正常工作:
通过
pnputil -i -a file.inf安装驱动程序。
(如果提升,这在 PowerShell 和命令提示符中都非常有效。)通过 PowerShell 添加打印机驱动程序。
通过 PowerShell 添加打印机端口。
通过 PowerShell 添加打印机。
如果我通过手动运行 #1 来添加驱动程序到存储,我的代码效果很好。但无论出于何种原因,我都无法让 ps 或 cmd 从 C# 运行相同的命令(成功)。它返回错误Driver not in store。这是我的沙盒应用程序代码。
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Collections.ObjectModel;
using System.Management;
using System.Net;
using System.Net.NetworkInformation;
using System.Threading;
namespace NetworkPrinterDriver
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
Runspace rs;
public MainWindow()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
// Create OpenFileDialog
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
// Set filter for file extension and default file extension
dlg.DefaultExt = ".exe";
dlg.Filter = "Information Files (.inf)|*.inf";
//dlg.Filter = "Executables (.exe)|*.exe";
// Display OpenFileDialog by calling ShowDialog method
Nullable<bool> result = dlg.ShowDialog();
// Get the selected file name and display in a TextBox
if (result == true)
{
// Open document
string filename = dlg.FileName;
FileNameTextBox.Text = filename;
}
}
private void btnInstall_Click(object sender, RoutedEventArgs e)
{
driverInstall(FileNameTextBox.Text);
AddPrinterDriver(txtDriverName.Text);
AddPrinterPort(txtPortName.Text, txtHostIP.Text);
AddPrinter(txtPrinterName.Text, txtDriverName.Text, txtPortName.Text);
}
private void driverInstall(string driverPath)
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Verb = "runas";
startInfo.UseShellExecute = true;
startInfo.Arguments = string.Format("/C pnputil -i -a \"{0}\"", driverPath);
process.StartInfo = startInfo;
process.Start();
System.Threading.Thread.Sleep(500);
}
private void AddPrinterPort(string portName, string printerAddress)
{
string script = string.Format("Add-printerport -Name \"{0}\" -PrinterHostAddress \"{1}\"", portName, printerAddress);
RunScript(script);
}
private void AddPrinterDriver(string driverName)
{
string script = string.Format("Add-printerdriver -Name \"{0}\"", driverName);
RunScript(script);
}
private void AddPrinter(string printerName, string driverName, string portName)
{
string script = string.Format("Add-printer -Name \"{0}\" -DriverName \"{1}\" -Port \"{2}\"", printerName, driverName, portName);
RunScript(script);
}
private void RunScript(string script)
{
rs = RunspaceFactory.CreateRunspace();
rs.Open();
using (PowerShell ps = PowerShell.Create())
{
ps.AddScript(script);
ps.Runspace = rs;
ps.Invoke();
foreach (ErrorRecord err in ps.Streams.Error)
{
MessageBox.Show(err.ToString());
}
}
// rs.Close();
}
} // class MainWindow
} // namespace NetworkPrinterDriver
谁能向我解释我做错了什么?
cmd 提示符确实运行(Windows 要求我允许它进行更改),但它没有安装驱动程序。如果我从应用程序复制相同的字符串并粘贴到提升的命令提示符中,它可以工作。任何帮助表示赞赏。
【问题讨论】:
-
Powershell 不应该也被提升吗?
标签: c# powershell cmd process runas