Example #1
0
        private void getPCbtn_Click(object sender, EventArgs e)
        {
            try {
                PCinfoModel thisPC = scan.getPcInfo();

                string fileName = Interaction.InputBox("請輸入你的中文姓名與英文名子", "輸入名稱", "帥哥-FBI");
                fileName += "-" + DateTime.Now.ToString("yyyyMMdd") + ".txt";


                using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\" + fileName)) {
                    file.WriteLine("CPU: " + thisPC.proesser);
                    file.WriteLine("MotherBoard: " + thisPC.motherBoard);

                    int i = 0;
                    foreach (RamModel thisRam in thisPC.rams)
                    {
                        i++;
                        if (thisRam.type == 0)
                        {
                            thisRam.getTypeBySpeed(thisRam);
                        }

                        file.WriteLine("RAM" + i + ": " + thisRam.Maker + " " +
                                       Enum.GetName(typeof(RamModel.MemeryType), thisRam.type) + " " +
                                       thisRam.speed + " " +
                                       thisRam.getCapcityUnit(thisRam.capacity));
                    }
                    i = 0;
                    foreach (deviceModle thisDrive in thisPC.drives)
                    {
                        i++;
                        file.WriteLine("Drive" + i + ": " + thisDrive.modleName + " " + thisDrive.Size);
                    }


                    file.WriteLine("Graphic Card: " + thisPC.videoCard);
                    file.WriteLine("OS: " + thisPC.os);
                    file.WriteLine("----------------------------------------------------------------");
                }
            } catch (Exception ex) {
                System.Diagnostics.Trace.Assert(false, ex.ToString());
                string debugTxetPath = "C:\\getPCbtn_ClickDebug.txt";
                using (TextWriterTraceListener _DebugLog = new TextWriterTraceListener(debugTxetPath)) {
                    Trace.Listeners.Add(_DebugLog);
                    Trace.AutoFlush = true;
                    Trace.WriteLine("Button Area error:" + ex.StackTrace.ToString());
                    Trace.Listeners.Clear();
                    Trace.Close();
                }
            }

            MessageBox.Show("Thank you! I got you!", "U R the best");
            Process.Start(@"C:\");
        }
        public PCinfoModel getPcInfo()
        {
            PCinfoModel thisPC = new PCinfoModel();

            try {
                if (new ManagementObjectSearcher(@"SELECT * FROM Win32_Processor") == null || new ManagementObjectSearcher(@"SELECT * FROM Win32_Processor").Get().Count <= 0)
                {
                    thisPC.proesser = "No CPU";
                }
                else
                {
                    foreach (var CPU in new ManagementObjectSearcher(@"SELECT * FROM Win32_Processor").Get())
                    {
                        thisPC.proesser = CPU["Name"].ToString();
                    }
                }

                if (new ManagementObjectSearcher(@"SELECT * FROM Win32_BaseBoard") == null || new ManagementObjectSearcher(@"SELECT * FROM Win32_BaseBoard").Get().Count <= 0)
                {
                    thisPC.motherBoard = "No motherBoard";
                }
                else
                {
                    foreach (var MTB in new ManagementObjectSearcher(@"SELECT * FROM Win32_BaseBoard").Get())
                    {
                        thisPC.motherBoard = MTB["Manufacturer"].ToString() + " " + MTB["Product"].ToString();
                    }
                }

                int i = 0;
                if (new ManagementObjectSearcher(@"Select * From Win32_PhysicalMemory") == null || new ManagementObjectSearcher(@"Select * From Win32_PhysicalMemory").Get().Count <= 0)
                {
                    RamModel thisRam = new RamModel();
                    thisRam.Maker = "No Ram";
                    thisPC.rams.Add(thisRam);
                }
                else
                {
                    foreach (var RAM in new ManagementObjectSearcher(@"Select * From Win32_PhysicalMemory").Get())
                    {
                        RamModel thisRam = new RamModel();
                        thisRam.Maker    = Convert.ToString(RAM["Manufacturer"]);
                        thisRam.speed    = Convert.ToString(RAM["Speed"]) + "MHz";
                        thisRam.type     = Convert.ToInt32(RAM["MemoryType"]);
                        thisRam.capacity = Convert.ToInt64(RAM["Capacity"]);

                        thisPC.rams.Add(thisRam);
                    }
                }

                if (new ManagementObjectSearcher(@"Select * From Win32_DiskDrive") == null || new ManagementObjectSearcher(@"Select * From Win32_DiskDrive").Get().Count <= 0)
                {
                    deviceModle thisdevice = new deviceModle();
                    thisdevice.modleName = "No Drive";
                    thisPC.drives.Add(thisdevice);
                }
                else
                {
                    foreach (var Drive in new ManagementObjectSearcher(@"Select * From Win32_DiskDrive").Get())
                    {
                        deviceModle thisdevice = new deviceModle();

                        thisdevice.modleName = Convert.ToString(Drive["Model"]) + ++i;

                        if (Convert.ToInt64(Drive["Size"]) > 0)
                        {
                            thisdevice.Size = Math.Round((Convert.ToInt64(Drive["Size"]) / Math.Pow(1024, 3)), 3) + "GB";
                        }


                        thisPC.drives.Add(thisdevice);
                    }
                }

                if (new ManagementObjectSearcher(@"Select * From Win32_VideoController") == null || new ManagementObjectSearcher(@"Select * From Win32_VideoController").Get().Count <= 0)
                {
                    thisPC.videoCard = "No GPU";
                }
                else
                {
                    foreach (var GPU in new ManagementObjectSearcher(@"Select * From Win32_VideoController").Get())
                    {
                        thisPC.videoCard = Convert.ToString(GPU["Name"]);
                    }
                }

                if (new ManagementObjectSearcher(@"Select * From Win32_OperatingSystem").Get() == null || new ManagementObjectSearcher(@"Select * From Win32_OperatingSystem").Get().Count <= 0)
                {
                    thisPC.os = "No OS";
                }
                else
                {
                    foreach (var OS in new ManagementObjectSearcher(@"Select * From Win32_OperatingSystem").Get())
                    {
                        if (String.IsNullOrEmpty(Convert.ToString(OS["Name"])) || Convert.ToString(OS["Name"]).Split().Length < 1)
                        {
                            thisPC.os = Convert.ToString(OS["Name"]) + " " + Convert.ToString(OS["OSArchitecture"]) + " ";
                        }
                        else
                        {
                            thisPC.os = Convert.ToString(OS["Name"]).Split('|')[0] + " " + Convert.ToString(OS["OSArchitecture"]) + " ";
                        }


                        string version = "";

                        #region Switch OS Name case
                        switch (Convert.ToString(OS["BuildNumber"]))
                        {
                        case "2600":
                            version = "Windows XP";
                            break;

                        case "2600.1105-1106":
                            version = "Windows XP, Service Pack 1";
                            break;

                        case "2600.218":
                            version = "Windows XP, Service Pack 2";
                            break;

                        case "3790.118":
                            version = "Windows Server 2003, Service Pack 1";
                            break;

                        case "3790":
                            version = "Windows Server 2003 R2";
                            break;

                        case "6000":
                            version = "Windows Vista";
                            break;

                        case "4500":
                            version = "Windows Home Server";
                            break;

                        case "6001":
                            version = "Windows Vista, Service Pack 1";
                            break;

                        case "6002":
                            version = "Windows Vista, Service Pack 2";
                            break;

                        case "7600":
                            version = "Windows 7";
                            break;

                        case "7601":
                            version = "Windows Server 2008 R2, Service Pack 1";
                            break;

                        case "8400":
                            version = "Windows Home Server 2011";
                            break;

                        case "9200":
                            version = "Windows 8";
                            break;

                        case "9600":
                            version = "Windows 8.1";
                            break;

                        case "10240":
                            version = "Windows 10, Version 1507";
                            break;

                        case "10586":
                            version = "Windows 10, Version 1511";
                            break;

                        case "14393":
                            version = "Windows 10, Version 1607";
                            break;

                        case "15063":
                            version = "Windows 10, Version 1703";
                            break;

                        case "16299":
                            version = "Windows 10, Version 1709";
                            break;

                        case "17134":
                            version = "Windows 10, Version 1803";
                            break;

                        case "17763":
                            version = "Windows 10, Version 1809";
                            break;

                        case "6003":
                            version = "Windows Server 2008, Service Pack 2, Rollup KB4489887";
                            break;

                        case "18362":
                            version = "Windows 10, Version 1903";
                            break;

                        case "18363":
                            version = "Windows 10, Version 1909";
                            break;

                        case "19041":
                            version = "Windows 10, Version 2004";
                            break;

                        case "19042":
                            version = "Windows 10, Version 20H2";
                            break;

                        default:
                            version = "Can't Find " + OS["BuildNumber"];
                            break;
                        }
                        #endregion

                        thisPC.os += version;
                    }
                }
            } catch (Exception ex) {
                System.Diagnostics.Trace.Write(ex.ToString());

                System.Diagnostics.Trace.Assert(false, ex.ToString());
                string debugTxetPath = "C:\\getPcInfo.txt";
                using (TextWriterTraceListener _DebugLog = new TextWriterTraceListener(debugTxetPath)) {
                    Trace.Listeners.Add(_DebugLog);
                    Trace.AutoFlush = true;
                    Trace.WriteLine("SQL Area error:" + ex.StackTrace.ToString() + ex.Source.ToString() + ex.Data.ToString());
                    Trace.WriteLine(ex.ToString());
                    Trace.Write(thisPC.proesser + "\n" + thisPC.motherBoard + "\n" + thisPC.rams.ToArray() + "\n" + thisPC.drives.ToArray() + "\n" + thisPC.videoCard + "\n" + thisPC.os);
                    Trace.Listeners.Clear();
                    Trace.Close();
                }

                throw ex;
            }

            return(thisPC);
        }