Esempio n. 1
0
        private static List <string> GetAllPrintersInternal(int failCounter)
        {
            try
            {
                List <string> printers = new List <string>();

                string selectAllQuery = "SELECT * FROM Win32_Printer";

                System.Management.ObjectQuery oq = new System.Management.ObjectQuery(selectAllQuery);

                System.Management.ManagementObjectSearcher   query1           = new System.Management.ManagementObjectSearcher(oq);
                System.Management.ManagementObjectCollection queryCollection1 = query1.Get();

                foreach (System.Management.ManagementObject mo in queryCollection1)
                {
                    System.Management.PropertyDataCollection pdc = mo.Properties;
                    printers.Add(mo["Name"].ToString());
                }

                return(printers);
            }
            catch
            {
                if (failCounter > 3)
                {
                    throw;
                }

                Thread.Sleep(new TimeSpan(0, 0, 5));
                return(GetAllPrintersInternal(++failCounter));
            }
        }
Esempio n. 2
0
 /// <summary>
 /// Returns a CSV-formatted string containing the requested WMI table.
 /// </summary>
 /// <param name="inKey">WMI target table</param>
 /// <returns>CSV string</returns>
 public string readWMIKeyAsCSV(string inKey)
 {
     System.Text.StringBuilder sbOut = new System.Text.StringBuilder();
     System.Management.ManagementObjectSearcher mbs = new System.Management.ManagementObjectSearcher("SELECT * FROM " + inKey);
     try {
         foreach (System.Management.ManagementObject mo in mbs.Get())
         {
             System.Management.PropertyDataCollection searcherProperties = mo.Properties;
             foreach (System.Management.PropertyData sp in searcherProperties)
             {
                 sbOut.AppendLine(sp.Name + ";" + sp.Value);
             }
         }
     } catch (Exception ex) {
         sbOut.AppendLine("Error;" + ex.Message);
     }
     return(sbOut.ToString());
 }
Esempio n. 3
0
 /// <summary>
 /// Reads a specified value from any WMI table.
 /// </summary>
 /// <param name="inKey">WMI target table</param>
 /// <param name="inAttribute">WMI attribute to read</param>
 /// <returns>Value as object</returns>
 public object readWMICompound(string inKey, string inAttribute)
 {
     System.Management.ManagementObjectSearcher mbs = new System.Management.ManagementObjectSearcher("SELECT * FROM " + inKey);
     try {
         foreach (System.Management.ManagementObject mo in mbs.Get())
         {
             System.Management.PropertyDataCollection searcherProperties = mo.Properties;
             foreach (System.Management.PropertyData sp in searcherProperties)
             {
                 if (sp.Name.ToLower() == inAttribute.ToLower())
                 {
                     return(sp.Value);
                 }
             }
         }
         return(null);
     } catch (Exception ex) {
         return("Error: " + ex.Message);
     }
 }
Esempio n. 4
0
        public static void SetNewDefaultPrinter(string newDefaultPrinterName)
        {
            string selectAllQuery = "SELECT * FROM Win32_Printer";

            System.Management.ObjectQuery oq = new System.Management.ObjectQuery(selectAllQuery);

            System.Management.ManagementObjectSearcher   query1           = new System.Management.ManagementObjectSearcher(oq);
            System.Management.ManagementObjectCollection queryCollection1 = query1.Get();
            System.Management.ManagementObject           newDefault       = null;

            foreach (System.Management.ManagementObject mo in queryCollection1)
            {
                System.Management.PropertyDataCollection pdc = mo.Properties;
                if (mo["Name"].ToString().ToUpper().Trim() == newDefaultPrinterName.ToUpper())
                {
                    newDefault = mo;
                    break;
                }
            }

            if (newDefault != null)
            {
                System.Management.ManagementBaseObject outParams = newDefault.InvokeMethod("SetDefaultPrinter", null, null);
                string returnValue = outParams["returnValue"].ToString();
                if (returnValue != "0")
                {
                    throw new ApplicationException(string.Format("Change Printer '{0}' failed. Return Value: {1}", newDefault.ToString(), returnValue));
                }

                var     HWND_BROADCAST   = new IntPtr(0xffff);
                uint    WM_SETTINGCHANGE = 0x001A;
                UIntPtr innerPinvokeResult;
                var     pinvokeResult = SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, UIntPtr.Zero,
                                                           IntPtr.Zero, SendMessageTimeoutFlags.SMTO_NORMAL, 1000, out innerPinvokeResult);
            }
            else
            {
                throw new ApplicationException(string.Format("Change Printer '{0}' failed. Managemengt object not found", newDefaultPrinterName));
            }
        }
Esempio n. 5
0
 /// <summary>
 /// Get a list of important WMI tables plus all names and values.
 /// </summary>
 /// <returns>String</returns>
 public string listImportantCompounds()
 {
     System.Text.StringBuilder sbOut = new System.Text.StringBuilder();
     for (int i = 0; i < itemList.Length; i++)
     {
         sbOut.AppendLine("===== ===== ===== ===== ===== ===== ===== ===== ===== =====");
         sbOut.AppendLine(itemList[i].ToUpper() + ":\n");
         System.Management.ManagementObjectSearcher mbs = new System.Management.ManagementObjectSearcher("SELECT * FROM " + itemList[i]);
         try {
             foreach (System.Management.ManagementObject mo in mbs.Get())
             {
                 System.Management.PropertyDataCollection searcherProperties = mo.Properties;
                 foreach (System.Management.PropertyData sp in searcherProperties)
                 {
                     sbOut.AppendLine(sp.Name + " = '" + sp.Value + "'");
                 }
             }
         } catch (Exception ex) {
             sbOut.AppendLine("Error: " + ex.Message);
         }
     }
     return(sbOut.ToString());
 }
Esempio n. 6
0
 private static IEnumerable <System.Management.PropertyData> getPropertiesList(System.Management.PropertyDataCollection propertyCollection)
 {
     foreach (var p in propertyCollection)
     {
         yield return(p);
     }
 }