【问题标题】:How to run PnPUtil as admin from with C# application?如何使用 C# 应用程序以管理员身份运行 PnPUtil?
【发布时间】:2018-11-01 12:34:38
【问题描述】:

我有一个指令可以从 C# (WPF) 应用程序以编程方式安装打印机驱动程序。不得允许用户退出我的应用程序并从 Windows 安装。我已经设法通过手动使其正常工作:

  1. 通过pnputil -i -a file.inf安装驱动程序。
    (如果提升,这在 PowerShell 和命令提示符中都非常有效。)

  2. 通过 PowerShell 添加打印机驱动程序。

  3. 通过 PowerShell 添加打印机端口。

  4. 通过 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


【解决方案1】:

已解决:

我将其更改为使用提升的 powershell cmd:

 var newProcessInfo = new System.Diagnostics.ProcessStartInfo();
 newProcessInfo.FileName = @"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe";
 newProcessInfo.Verb = "runas";
 newProcessInfo.Arguments = string.Format("pnputil -i -a \"{0}\"", driverPath);
 System.Diagnostics.Process.Start(newProcessInfo);

感谢@Aybe 的提示。奇迹般有效。追兔子.......

【讨论】:

    猜你喜欢
    • 2011-12-29
    • 1970-01-01
    • 1970-01-01
    • 2011-09-24
    • 2010-12-19
    • 1970-01-01
    • 2013-06-17
    • 2013-05-31
    相关资源
    最近更新 更多