我有一个从C#(WPF)应用程序以编程方式安装打印机驱动程序的指令。不得允许用户退出我的应用程序并从Windows安装。我已设法通过手动实现这一目标:
通过pnputil -i -a file.inf
安装驱动程序。
(如果提升,这在PowerShell和命令提示符下都很有效。)
通过PowerShell添加打印机驱动程序。
通过PowerShell添加打印机端口。
通过PowerShell添加打印机。
如果我通过手动运行#1将驱动程序添加到商店,我的代码效果很好。但无论出于何种原因我都无法得到ps或cmd从C#运行相同的命令(成功)。它返回错误驱动程序不在商店。这是来自沙盒应用程序的代码。
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要求我允许它进行更改),但它没有安装驱动程序。如果我从应用程序复制相同的字符串并粘贴到提升的命令提示符,它可以工作。任何帮助表示赞赏。
答案 0 :(得分:0)
解决:
我将其更改为使用提升的powershell cmd:
No sequencer available for 'win32' platform.
C:\Python27\lib\site-packages\setuptools\dist.py:341: UserWarning:
Normalizing 'v0.2.3' to '0.2.3'
normalized_version,
running install
running bdist_egg
running egg_info
creating midi.egg-info
writing midi.egg-info\PKG-INFO
writing top-level names to midi.egg-info\top_level.txt
writing dependency_links to midi.egg-info\dependency_links.txt
writing manifest file 'midi.egg-info\SOURCES.txt'
**error: package directory 'src' does not exist**
感谢@Aybe提示。奇迹般有效。追逐兔子.......