Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用C#应用程序以管理员身份运行PnPUtil?_C#_Powershell_Cmd_Process_Runas - Fatal编程技术网

如何使用C#应用程序以管理员身份运行PnPUtil?

如何使用C#应用程序以管理员身份运行PnPUtil?,c#,powershell,cmd,process,runas,C#,Powershell,Cmd,Process,Runas,我有一个从C#(WPF)应用程序以编程方式安装打印机驱动程序的指令。不得允许用户退出我的应用程序并从Windows安装。我已通过手动方式使其工作正常: 通过pnputil-i-a file.inf安装驱动程序 (这在PowerShell和命令提示符(如果提升)中都非常有效。) 通过PowerShell添加打印机驱动程序 通过PowerShell添加打印机端口 通过PowerShell添加打印机 如果我通过手动运行#1将驱动程序添加到存储中,我的代码工作得很好。但无论出于何种原因,我都无法从C#获

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

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

  • 通过PowerShell添加打印机驱动程序

  • 通过PowerShell添加打印机端口

  • 通过PowerShell添加打印机

  • 如果我通过手动运行#1将驱动程序添加到存储中,我的代码工作得很好。但无论出于何种原因,我都无法从C#获得ps或cmd来运行相同的命令(成功)。它返回时出现错误驱动程序不在存储中。这是我在沙盒应用程序中的代码

    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
    
    使用系统;
    使用System.Collections.Generic;
    使用系统诊断;
    使用System.Linq;
    使用系统文本;
    使用System.Threading.Tasks;
    使用System.Windows;
    使用System.Windows.Controls;
    使用System.Windows.Data;
    使用System.Windows.Documents;
    使用System.Windows.Input;
    使用System.Windows.Media;
    使用System.Windows.Media.Imaging;
    使用System.Windows.Navigation;
    使用System.Windows.Shapes;
    使用系统、管理、自动化;
    使用System.Management.Automation.Runspaces;
    使用System.Collections.ObjectModel;
    使用制度管理;
    Net系统;
    使用System.Net.NetworkInformation;
    使用系统线程;
    命名空间网络PrinterDriver
    {
    /// 
    ///MainWindow.xaml的交互逻辑
    /// 
    公共部分类主窗口:窗口
    {
    运行空间;
    公共主窗口()
    {
    初始化组件();
    }
    私有无效按钮1\u单击(对象发送者,路由目标)
    {
    //创建OpenFileDialog
    Microsoft.Win32.OpenFileDialog dlg=新的Microsoft.Win32.OpenFileDialog();
    //为文件扩展名和默认文件扩展名设置筛选器
    dlg.DefaultExt=“.exe”;
    dlg.Filter=“信息文件(.inf)|*.inf”;
    //dlg.Filter=“可执行文件(.exe)|*.exe”;
    //通过调用ShowDialog方法显示OpenFileDialog
    可为空的结果=dlg.ShowDialog();
    //获取所选文件名并显示在文本框中
    如果(结果==真)
    {
    //打开文件
    字符串文件名=dlg.filename;
    FileNameTextBox.Text=文件名;
    }
    }
    私有无效BTN安装\单击(对象发送方,路由目标)
    {
    driverInstall(FileNameTextBox.Text);
    AddPrinterDriver(txtDriverName.Text);
    AddPrinterPort(txtPortName.Text,txtHostIP.Text);
    AddPrinter(txtPrinterName.Text、txtDriverName.Text、txtPortName.Text);
    }
    私有void driverInstall(字符串driverPath)
    {
    System.Diagnostics.Process Process=新的System.Diagnostics.Process();
    System.Diagnostics.ProcessStartInfo startInfo=新系统.Diagnostics.ProcessStartInfo();
    startInfo.WindowStyle=System.Diagnostics.ProcessWindowStyle.Hidden;
    startInfo.FileName=“cmd.exe”;
    startInfo.Verb=“runas”;
    startInfo.UseShellExecute=true;
    startInfo.Arguments=string.Format(“/cpnputil-i-a\{0}\”,driverPath);
    process.StartInfo=StartInfo;
    process.Start();
    系统.线程.线程.睡眠(500);
    }
    私有void AddPrinterPort(字符串端口名、字符串打印机地址)
    {
    string script=string.Format(“添加printerport-Name\“{0}\”-PrinterHostAddress\“{1}\”,端口名,printerAddress);
    运行脚本(脚本);
    }
    私有void AddPrinterDriver(字符串驱动程序名)
    {
    string script=string.Format(“addprinterdriver-Name\“{0}\”,driverName);
    运行脚本(脚本);
    }
    私有void AddPrinter(字符串printerName、字符串driverName、字符串portName)
    {
    string script=string.Format(“添加打印机-名称\“{0}\”-驱动器名\“{1}\”-端口\“{2}\”,打印机名,驱动器名,端口名);
    运行脚本(脚本);
    }
    私有void运行脚本(字符串脚本)
    {
    rs=RunspaceFactory.CreateRunspace();
    rs.Open();
    使用(PowerShell ps=PowerShell.Create())
    {
    ps.AddScript(脚本);
    ps.运行空间=rs;
    ps.Invoke();
    foreach(在ps.Streams.Error中记录错误)
    {
    Show(err.ToString());
    }
    }
    //rs.Close();
    }
    }//类主窗口
    }//名称空间网络PrinterDriver
    
    有人能解释一下我做错了什么吗

    cmd提示符确实运行(Windows要求我允许它进行更改),但它没有安装驱动程序。如果我从应用程序复制相同的字符串并粘贴到提升的命令提示符中,它就会工作。感谢您的帮助。

    已解决:

    我将此更改为使用提升的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的提示。工作起来很有魅力。追逐兔子……

    动力壳不也应该升高吗?