列出每个用户的所有网络打印机

Posted

技术标签:

【中文标题】列出每个用户的所有网络打印机【英文标题】:List all network printers per user 【发布时间】:2013-04-04 19:04:38 【问题描述】:

在我的工作中,我们为每个用户安装打印机。我正在尝试编写一个备份程序,该程序将列出所有给定用户名的联网打印机。那有可能吗?通过 WMI 或 System.IO 就可以了。这是列出所有机器联网打印机的代码,但不是每个用户的代码。

private void Button_Click(object sender, EventArgs e)

        ConnectionOptions objConnection = new ConnectionOptions();

        objConnection.Impersonation = ImpersonationLevel.Impersonate;

        objConnection.EnablePrivileges = true;



         string backupselectedcomputer = "Some Computer Name"

          ManagementScope objScope = new ManagementScope("\\\\" + backupselectedcomputer     +"\\root\\cimv2",objConnection); 
         objScope.Connect();

         SelectQuery selectQuery = new SelectQuery();
         selectQuery.QueryString = "Select * from win32_Printer Where Local = FALSE";
         ManagementObjectSearcher MOS = new ManagementObjectSearcher(objScope, selectQuery);
         ManagementObjectCollection MOC = MOS.Get();

        foreach (ManagementObject mo in MOC)

        

       //lbBackupprinters is a list box
            lbBackupprinters.Items.Add(mo["Name"].ToString().ToUpper());
         

 

【问题讨论】:

我已经编辑了你的标题。请参阅“Should questions include “tags” in their titles?”,其中的共识是“不,他们不应该”。 如果是按用户分配的,你肯定是在活动目录中添加了打印机,尝试在那里找到... ***.com/questions/10468139/… 我们使用 VBS 脚本从打印服务器而不是 AD 将打印机添加到基于每个用户的计算机。看起来我将不得不通过注册表远程解析并找到用户 SID,然后通过 HKCU 或 HKUSERS 并从那里获取它 【参考方案1】:

想出了一个办法。这是遇到此问题的任何人的代码。

 private void ddlBackupselectuser_SelectionChangeCommitted(object sender, EventArgs e)
    

        lbBackupprinters.Items.Clear();

        string selecteduser = ddlBackupselectuser.Text;

        string computer = ddlBackupselectcomp.Text;

        string sid;

        lblBackuppwd.Visible = true;
        txtBackuppwd.Visible = true;
        cboBackuppwdshow.Visible = true;

        //BEGIN GRAB PRINTERS FROM REGISTRY

        try
        
            NTAccount ntuser = new NTAccount(selecteduser);
            SecurityIdentifier sID = (SecurityIdentifier)ntuser.Translate(typeof(SecurityIdentifier));
            lblBackupStatus.Text = sID.ToString();
            sid = sID.ToString();
        

        catch (IdentityNotMappedException)
            
                lblBackupStatus.Text = "ERROR "+ ddlBackupselectuser.Text.ToString() + " contains no profile information";

                lbBackupprinters.Items.Add("No Printers Found");

            return;

            



                 ConnectionOptions co = new ConnectionOptions();
                 co.EnablePrivileges = true;
                 co.Impersonation = ImpersonationLevel.Impersonate;

                 System.Net.IPHostEntry h = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName());
                 string IPAddress = h.AddressList.GetValue(0).ToString();
                 string lm = System.Net.Dns.GetHostName().ToString();

                try
                 


                     ManagementScope myScope = new ManagementScope("\\\\" + computer + "\\root\\default", co);
                     ManagementPath mypath = new ManagementPath("StdRegProv");
                     ManagementClass wmiRegistry = new ManagementClass(myScope, mypath, null);


                    const uint HKEY_LOCAL_MACHINE = unchecked((uint)0x80000002);
                     //ManagementClass wmiRegistry = new ManagementClass("root/default",
                     //"StdRegProv",null);

                     string keyPath = @"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Print\\Providers\\Client Side Rendering Print Provider\\" + sid + "\\Printers\\Connections";
                     object[] methodArgs = new object[]  HKEY_LOCAL_MACHINE, keyPath, null ;
                     uint returnValue = (uint)wmiRegistry.InvokeMethod("EnumKey", methodArgs);


                     if (null != methodArgs[2])
                     
                         string[] subKeys = methodArgs[2] as String[];

                         if (subKeys == null)
                         
                             lbBackupprinters.Items.Add("No Printers Found");
                             return;
                         

                         ManagementBaseObject inParam = wmiRegistry.GetMethodParameters("GetStringValue");
                         inParam["hDefKey"] = HKEY_LOCAL_MACHINE;


                         string keyName = "";


                         foreach (string subKey in subKeys)
                         
                             //Display application name
                             keyPath = @"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Print\\Providers\\Client Side Rendering Print Provider\\" + sid + "\\Printers\\Connections" + subKey;
                             keyName = "DisplayName";
                             inParam["sSubKeyName"] = keyPath;
                             inParam["sValueName"] = keyName;
                             ManagementBaseObject outParam = wmiRegistry.InvokeMethod("GetStringValue", inParam, null);



                                 lbBackupprinters.Items.Add(subKey);


                         
                     

                     else
                     

                         lbBackupprinters.Items.Add("No Printers Found");                             

                     
                 
                 catch (Exception ex)
                 
                     MessageBox.Show("Error: " + ex.Message);
                

【讨论】:

哇,这行得通!它甚至可以让我通过服务访问打印机列表!

以上是关于列出每个用户的所有网络打印机的主要内容,如果未能解决你的问题,请参考以下文章

Java - 列出 LAN 上的所有网络打印机

使用 ListBox Powershell 列出每台网络打印机

列出可用的网络打印机(也未安装)[重复]

Powershell 列出远程 PC 上的网络打印机

HP LaserJet P2055dn 通过网络连接打印机用户指南

共享打印机出现“输入网络密码、登录失败、未知的用户名或密码错误!