private static string GetProperty(ManagementBaseObject obj, string property)
        {
            var value = (obj.GetPropertyValue(property) == null)
                            ? "NA"
                            : obj.GetPropertyValue(property).ToString();

            return value;
        }
 private bool IsPriority(System.Management.ManagementBaseObject obj)
 {
     if ((string)obj.GetPropertyValue("PropertyName") == "Priority")
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
        private Win32UsbControllerDevice GetDevice(ManagementBaseObject managementObject)
        {
            try
            {
                Win32UsbControllerDevice win32UsbControllerDevice = new Win32UsbControllerDevice();

                try
                {
                    String dependent = managementObject.GetPropertyValue("Dependent").ToString();
                    win32UsbControllerDevice.DeviceId = this.ExtractSeviceId(dependent);
                }
                catch (Exception ex)
                {
                }

                try
                {
                    String antecedent = managementObject.GetPropertyValue("Antecedent").ToString();
                    win32UsbControllerDevice.ControllerId = this.ExtractSeviceId(antecedent);
                }
                catch (Exception ex)
                {
                }

                try
                {
                    String locationInformation = this.GetLocationInformation(win32UsbControllerDevice.DeviceId);

                    Int32 hubIndex = locationInformation.IndexOf("HUB_#", StringComparison.OrdinalIgnoreCase);
                    Int32 portIndex = locationInformation.IndexOf("PORT_#", StringComparison.OrdinalIgnoreCase);

                    if ((hubIndex < 0) || (portIndex < 0))
                    {
                        throw new Exception("Wrong location information format");
                    }

                    win32UsbControllerDevice.Hub = locationInformation.Substring(hubIndex + 5, 4);
                    win32UsbControllerDevice.Port = locationInformation.Substring(portIndex + 6, 4);
                }
                catch (Exception ex)
                {
                }

                try
                {
                    Int32 vidIndex = win32UsbControllerDevice.DeviceId.IndexOf("VID", StringComparison.OrdinalIgnoreCase);
                    Int32 pidIndex = win32UsbControllerDevice.DeviceId.IndexOf("PID", StringComparison.OrdinalIgnoreCase);

                    if ((vidIndex < 0) || (pidIndex < 0))
                    {
                        throw new Exception("Wrong device ID format");
                    }

                    win32UsbControllerDevice.Vid = win32UsbControllerDevice.DeviceId.Substring(vidIndex + 4, 4);
                    win32UsbControllerDevice.Pid = win32UsbControllerDevice.DeviceId.Substring(pidIndex + 4, 4);
                }
                catch (Exception ex)
                {
                }

                return win32UsbControllerDevice;
            }
            catch (Exception ex)
            {
            }

            return null;
        }
Exemple #4
0
        /// <summary>
        /// Get the IP of a printer
        /// </summary>
        /// <param name="printer"></param>
        /// <returns></returns>
        private string GetPrinterIP(ManagementBaseObject printer)
        {
            // Select a Win32_TCPIPPrinterPort with a matching name as the printer's PortName
            // This will allow us to get the actual address being used by the port
            using (var query = new ManagementObjectSearcher(
                $"SELECT * from Win32_TCPIPPrinterPort WHERE Name LIKE \"{printer.GetPropertyValue("PortName")}\""))
            {
                foreach (var port in query.Get())
                    return port.GetPropertyValue("HostAddress").ToString();
            }

            return null;
        }
Exemple #5
0
        /// <summary>
        /// Get the path to the inf used by a printer
        /// </summary>
        /// <param name="printer"></param>
        /// <returns></returns>
        private string GetPrinterDriver(ManagementBaseObject printer)
        {
            // Get the name of the driver used by the printer and match it to a Win32_PrinterDriver
            var name = printer.GetPropertyValue("DriverName");

            using (var query = new ManagementObjectSearcher("SELECT * from Win32_PrinterDriver"))
            {
                // Select only the drivers which match the naming scheme (driver is in CSV format name,version,32/64bit)
                foreach (var driver in from ManagementBaseObject driver 
                                       in query.Get()
                                       let info = driver.GetPropertyValue("Name").ToString().Split(',')
                                       where info[0].Equals(name)
                                       select driver)
                {
                    // TODO: Rewrite using ternary or null coalescing
                    try
                    {
                        return Path.Combine(driver.GetPropertyValue("FilePath").ToString(),
                            driver.GetPropertyValue("InfName").ToString());
                    }
                    catch (Exception)
                    {
                        return @"C:\Windows\inf\ntprint.inf";
                    }
                }
            }

            return null;
        }
Exemple #6
0
        internal Game(System.Management.ManagementBaseObject game)
        {
            this.name       = game.GetPropertyValue("Name") as string;
            this.instanceID = game.GetPropertyValue("InstanceID") as string;
            this.gameID     = game.GetPropertyValue("GameID") as string;
            this.gdfPath    = game.GetPropertyValue("GDFBinaryPath") as string;
            string str1 = this.gdfPath;

            this.installPath   = game.GetPropertyValue("GameInstallPath") as string;
            this.gdfResourceID = game.GetPropertyValue("ResourceIDForGDFInfo") as string;
            this.installScope  = (Game.GameInstallScope)game.GetPropertyValue("InstallScope");
            this.RefreshTasks();
            if (this.gdfPath != null)
            {
                try
                {
                    using (UnmanagedLibrary unmanagedLibrary = new UnmanagedLibrary(this.gdfPath))
                    {
                        try
                        {
                            byte[] bytes = ResourceExtensions.GetBytes(unmanagedLibrary.GetResource("__GDF_THUMBNAIL", (object)"DATA"));
                            if (bytes != null)
                            {
                                try
                                {
                                    this.image = (ImageSource)BitmapDecoder.Create((Stream) new MemoryStream(bytes), BitmapCreateOptions.None, BitmapCacheOption.Default).Frames[0];
                                }
                                catch (Exception ex)
                                {
                                    Trace.TraceWarning("Image for " + this.name + "could not be loaded: " + ex.Message);
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Trace.TraceWarning("Error attempting to get image resource for " + this.name + "could not be loaded: " + ex.Message);
                        }
                        using (RegistryKey registryKey1 = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\GameUX\\"))
                        {
                            if (registryKey1 != null)
                            {
                                foreach (object obj in registryKey1.GetSubKeyNames())
                                {
                                    string      name         = string.Format("{0}\\{1}", obj, (object)this.instanceID);
                                    RegistryKey registryKey2 = registryKey1.OpenSubKey(name);
                                    if (registryKey2 != null)
                                    {
                                        using (registryKey2)
                                        {
                                            if (this.image == null)
                                            {
                                                string uriString = registryKey2.GetValue("BoxArt") as string;
                                                if (uriString != null)
                                                {
                                                    try
                                                    {
                                                        this.image = (ImageSource)BitmapDecoder.Create(new Uri(uriString), BitmapCreateOptions.None, BitmapCacheOption.Default).Frames[0];
                                                    }
                                                    catch (Exception ex)
                                                    {
                                                        Trace.TraceWarning("Image for legacy game " + this.name + "could not be loaded: " + ex.Message);
                                                    }
                                                }
                                            }
                                            this.description = registryKey2.GetValue("Description") as string;
                                            if (this.playTasks.Count == 0)
                                            {
                                                string str2 = registryKey2.GetValue("AppExePath") as string;
                                                if (str2 != null)
                                                {
                                                    this.playTasks.Add(str2);
                                                    str1 = str2;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        string name1  = this.gdfResourceID ?? "__GDF_XML";
                        byte[] bytes1 = ResourceExtensions.GetBytes(unmanagedLibrary.GetResource(name1, (object)"DATA"));
                        if (bytes1 != null)
                        {
                            XmlDocument xmlDocument = new XmlDocument();
                            xmlDocument.Load((XmlReader) new XmlTextReader((Stream) new MemoryStream(bytes1)));
                            foreach (XmlNode xmlNode in xmlDocument.DocumentElement.FirstChild.ChildNodes)
                            {
                                XmlElement xmlElement = xmlNode as XmlElement;
                                if (xmlElement != null && xmlElement.Name == "Description")
                                {
                                    this.description = xmlElement.InnerXml;
                                }
                            }
                        }
                        else
                        {
                            Trace.TraceWarning("GDF resource not found for " + this.Name + ".");
                        }
                    }
                }
                catch (Exception ex)
                {
                    Trace.TraceWarning("Error getting GDF data for " + this.name + "could not be loaded: " + ex.Message);
                }
            }
            if (this.playTasks.Count == 0)
            {
                this.IsValid       = false;
                this.InvalidReason = "Could not find a play task for " + this.name + ".";
                return;
            }
            if (this.image != null)
            {
                return;
            }
            string filename = (string)null;

            if (this.playTasks.Count > 0)
            {
                try
                {
                    WshShell shell = new WshShell();
                    filename = ((IWshShortcut)shell.CreateShortcut(this.playTasks[0])).TargetPath;
                }
                catch
                {
                }
            }
            if (string.IsNullOrEmpty(filename))
            {
                filename = str1;
            }
            if (string.IsNullOrEmpty(filename))
            {
                return;
            }
            this.image = Shell.GenerateThumbnail(filename);
        }
		private void ProcessPingStatus(ManagementBaseObject pingStatus)
		{
			string str = (string)LanguagePrimitives.ConvertTo(pingStatus.GetPropertyValue("Address"), typeof(string), CultureInfo.InvariantCulture);
			int num = (int)LanguagePrimitives.ConvertTo(pingStatus.GetPropertyValue("PrimaryAddressResolutionStatus"), typeof(int), CultureInfo.InvariantCulture);
			if (num == 0)
			{
				int num1 = (int)LanguagePrimitives.ConvertTo(pingStatus.GetPropertyValue("StatusCode"), typeof(int), CultureInfo.InvariantCulture);
				if (num1 == 0)
				{
					this.quietResults[str] = true;
					if (!this.quiet)
					{
						base.WriteObject(pingStatus);
					}
				}
				else
				{
					if (!this.quiet)
					{
						Win32Exception win32Exception = new Win32Exception(num1);
						string str1 = StringUtil.Format(ComputerResources.NoPingResult, str, win32Exception.Message);
						Exception pingException = new PingException(str1, win32Exception);
						ErrorRecord errorRecord = new ErrorRecord(pingException, "TestConnectionException", ErrorCategory.ResourceUnavailable, str);
						base.WriteError(errorRecord);
						return;
					}
				}
			}
			else
			{
				if (!this.quiet)
				{
					Win32Exception win32Exception1 = new Win32Exception(num);
					string str2 = StringUtil.Format(ComputerResources.NoPingResult, str, win32Exception1.Message);
					Exception exception = new PingException(str2, win32Exception1);
					ErrorRecord errorRecord1 = new ErrorRecord(exception, "TestConnectionException", ErrorCategory.ResourceUnavailable, str);
					base.WriteError(errorRecord1);
					return;
				}
			}
		}
                private static string SafeGetProperty(ManagementBaseObject mbo, string key)
                {
                    try {
                        object o = mbo.GetPropertyValue(key);
                        if(o != null) {
                            return o.ToString();
                        }
                    } catch {}

                    return "key is null";
                }