1

在我的工作中,我们为每个用户安装打印机。我正在尝试编写一个备份程序,该程序将列出所有给定用户名的联网打印机。那有可能吗?通过 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());
         }

 }
4

1 回答 1

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);
                }
于 2013-04-26T21:37:09.103 回答