Exemplo n.º 1
0
        public static string GetKeyQueryAsXmlString(KeyQueryModel keyQueryModel)
        {
            var serializer = new XmlSerializer(typeof(KeyQueryModel));
            var strBuilder = new Utf8StringWriter();
            var xmlWriter  = XmlWriter.Create(strBuilder);

            serializer.Serialize(xmlWriter, keyQueryModel);
            var outStr = strBuilder.ToString();

            return(outStr);
        }
Exemplo n.º 2
0
        public static KeyQueryModel GetCurrentKeyQuery()
        {
            var snm = new KeyQueryModel();

            var atr = Assembly.GetEntryAssembly().GetCustomAttributes(typeof(AssemblyProductAttribute), false)
                      .Single(a => a is AssemblyProductAttribute);


            var productAtr = (AssemblyProductAttribute)atr;

            if (string.IsNullOrWhiteSpace(productAtr.Product))
            {
                throw new Exception("У приложения должен быть реализован аттрибут AssemblyProductAttribute");
            }

            snm.ProductName = productAtr.Product;


            try
            {
                ManagementObjectSearcher searcher =
                    new ManagementObjectSearcher("root\\CIMV2",
                                                 "SELECT * FROM Win32_MotherboardDevice");
                var paramName = "SystemName";
                foreach (ManagementObject queryObj in searcher.Get())
                {
                    if (queryObj[paramName] != null && !string.IsNullOrWhiteSpace(queryObj[paramName].ToString()))
                    {
                        snm.ComputerName = queryObj[paramName].ToString();
                    }
                }
            }
            catch (ManagementException e)
            {
            }

            try
            {
                ManagementObjectSearcher searcher =
                    new ManagementObjectSearcher("root\\CIMV2",
                                                 "SELECT * FROM Win32_OperatingSystem");

                var paramName = "KeyQuery";
                foreach (ManagementObject queryObj in searcher.Get())
                {
                    if (queryObj[paramName] != null && !string.IsNullOrWhiteSpace(queryObj[paramName].ToString()))
                    {
                        var serial          = queryObj[paramName].ToString();
                        int replacemntCount = 5;
                        if (serial.Length > replacemntCount)
                        {
                            serial = (new int[replacemntCount]).Select(i => i.ToString())
                                     .Aggregate((i, i1) => i + i1) + serial.Substring(5);
                        }
                        snm.SystemSerial = serial;
                    }
                }
            }
            catch (ManagementException e)
            {
            }


            try
            {
                ManagementObjectSearcher searcher =
                    new ManagementObjectSearcher("root\\CIMV2",
                                                 "SELECT * FROM Win32_Processor");
                var paramName = "ProcessorId";
                foreach (ManagementObject queryObj in searcher.Get())
                {
                    if (queryObj[paramName] != null && !string.IsNullOrWhiteSpace(queryObj[paramName].ToString()))
                    {
                        snm.ProcessorId = queryObj[paramName].ToString();
                    }
                }
            }
            catch (ManagementException e)
            {
            }

            return(snm);
        }