获取局域网电脑的硬件配置

为了防止公司的电脑被人偷换硬件,老板又担心网管软件不安全,只能自己开发程序获取电脑配置,并且固定时间更新一次。

本来想用CS架构开发一个软件,但是本人对程序不是精通,只能采取适合自己的简单办法,直接通过客户端连接数据库,总之是局域网可以不考虑安全问题。

开发思路如下:

1 获取本地电脑的硬件配置

2 上传到SQL Server数据库

3 本地要有一个自动更新的程序,可以从服务器下载最新的硬件获取程序

下面是部分源代码

1 获取本地电脑的硬件配置

 private string getmem()
        {
            string iMem = null;
            ManagementObjectSearcher moSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMemory");

            foreach (ManagementObject wmi_HD in moSearcher.Get())
            {

                iMem += wmi_HD["Manufacturer"].ToString() + "/" + wmi_HD["Capacity"].ToString() + "/" + wmi_HD["SerialNumber"].ToString() + "/";

            }
            iMem = iMem.TrimEnd('/');
            return iMem;
        }

        private string getdisk()
        {
            string iDisk = null;
            ManagementObjectSearcher moSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");

            foreach (ManagementObject wmi_HD in moSearcher.Get())
            {

                iDisk += wmi_HD["Model"].ToString() + "/" + wmi_HD["Size"].ToString() + "/" + wmi_HD["SerialNumber"].ToString().Trim() + "/";

            }
            iDisk = iDisk.TrimEnd('/');
            return iDisk;
        }

        private string getcpu()
        {
            string iCpu = null;
            ManagementObjectSearcher moSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_Processor");

            foreach (ManagementObject wmi_HD in moSearcher.Get())
            {

                iCpu += wmi_HD["Manufacturer"].ToString() + "/" + wmi_HD["MaxClockSpeed"].ToString() + "/" + wmi_HD["ProcessorId"].ToString().Trim() + "/";

            }
            iCpu = iCpu.TrimEnd('/');
            return iCpu;
        }

        private string getboard()
        {
            string iBoard = null;
            ManagementObjectSearcher moSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_baseboard");

            foreach (ManagementObject wmi_HD in moSearcher.Get())
            {

                iBoard += wmi_HD["Manufacturer"].ToString() + "/" + wmi_HD["SerialNumber"].ToString() + "/" + wmi_HD["Product"].ToString().Trim() + "/";

            }
            iBoard = iBoard.TrimEnd('/');
            return iBoard;
        }

        private string getvideo()
        {
            string iVideo = null;
            try
            {
                ManagementObjectSearcher moSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_DesktopMonitor");

                foreach (ManagementObject wmi_HD in moSearcher.Get())
                {

                    iVideo += wmi_HD["PNPDeviceID"].ToString() + "/";

                }
                iVideo = iVideo.TrimEnd('/');
                //MessageBox.Show(iVideo);
                return iVideo;
            }
            catch { return "no video"; }
        }

2 上传到SQL Server数据库

 conn.Open();
                string sqlstr = "SELECT count(*) FROM DETAILS where name='" + b + "'";
                SqlCommand cmd = new SqlCommand(sqlstr, conn);
                int i = Convert.ToInt32(cmd.ExecuteScalar().ToString());
                if (i >= 1)
                {
                    //MessageBox.Show("有记录");
                    string sqlstr1 = "select Name,ICPU,IMEM,IDISK,IBOARD,IVideo,IDate,IFLAG,IP from DETAILS  where name='" + b + "'";
                    SqlCommand cmd1 = new SqlCommand(sqlstr1, conn);
                    SqlDataReader dr = cmd1.ExecuteReader();
                    if (dr.Read())
                    {
                        //MessageBox.Show(dr[0].ToString());
                        if (string.Equals(dr[1].ToString(), icpu) && string.Equals(dr[2].ToString(), imem) && string.Equals(dr[3].ToString(), idisk)
                            && string.Equals(dr[4].ToString(), iboard) && string.Equals(dr[5].ToString(), ivideo) && string.Equals(dr[8].ToString(), strAddr))
                        {
                            dr.Close();
                            string insSQL1 = @"update DETAILS set IDate = '" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "' where name='" + b + "'";
                            SqlCommand cmdinsert1 = new SqlCommand(insSQL1, conn);
                            int i1 = Convert.ToInt32(cmdinsert1.ExecuteNonQuery());
                            log.log("上传到服务器更新标志");
                        }// && dr[2].ToString == getmem() && dr[3].ToString == getdisk() && dr[4].ToString == getboard() && dr[7].ToString == strAddr
                        else
                        {
                            dr.Close();
                            string insSQL1 = @"insert into ERRORS (Name,ICPU,IMEM,IDISK,IBOARD,IVideo,IDate,IFLAG,IP) values ('" + b + "','" + icpu + "','" + imem + "','" + idisk + "','" + iboard + "','" + ivideo + "','" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "','0','" + strAddr + "')";
                            SqlCommand cmdinsert1 = new SqlCommand(insSQL1, conn);
                            int i1 = Convert.ToInt32(cmdinsert1.ExecuteNonQuery());
                            log.log("上传服务器更新错误记录");
                        }
                        //读取指定用户名对应的用户编号和密码
                        //string msg = "用户编号:" + dr[0] + " 密码:" + dr[1];
                        //将msg的值显示在标签上
                        //label2.Text = msg;
                    }
                    conn.Close();
                }
                else
                {
                    //MessageBox.Show("没有记录");
                    string insSQL = @"insert into DETAILS (Name,ICPU,IMEM,IDISK,IBOARD,IVideo,IDate,IFLAG,IP) values ('" + b + "','" + icpu + "','" + imem + "','" + idisk + "','" + iboard + "','" + ivideo + "','" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "','0','" + strAddr + "')";
                    SqlCommand cmdinsert = new SqlCommand(insSQL, conn);
                    int i1 = Convert.ToInt32(cmdinsert.ExecuteNonQuery());
                    conn.Close();
                }

3 本地要有一个自动更新的程序,可以从服务器下载最新的硬件获取程序

刚开始是加到了自启动,但是被360杀毒干掉了,后来用Windows 服务来实现,目前还比较稳定,我用了一个apache服务器,来存放需要更新的文件,然后通过标志,来判断更新

 File.Delete("C:\\ihardware\\Log.txt");
            using (WebClient wc = new WebClient())
            {

                wc.DownloadFileAsync(
                    // Param1 = Link of file
                    new System.Uri("http://192.168.1.195:8888/server.txt"),
                    // Param2 = Path to save
                    "C:\\ihardware\\server.txt"
                );
                Log log = new Log(AppDomain.CurrentDomain.BaseDirectory + @"/Log.txt");
                log.log("下载server.ext");
                Thread.Sleep(5000);
                string sflag = fileToString("C:\\ihardware\\server.txt");
                if (sflag.Length != 0)
                {
                    string cflag = fileToString("C:\\ihardware\\client.txt");
                    KillProcess("MyHardWare");
                    if (int.Parse(sflag) > int.Parse(cflag))
                    {

                        Thread.Sleep(2000);
                        File.Delete("C:\\ihardware\\MyHardWare.exe");

                        wc.DownloadFileAsync(
                            // Param1 = Link of file
                        new System.Uri("http://192.168.1.195:8888/MyHardWare.exe"),
                            // Param2 = Path to save
                        "C:\\ihardware\\MyHardWare.exe"
                        );
                        Thread.Sleep(5000);
                        //Log log = new Log(AppDomain.CurrentDomain.BaseDirectory + @"/Log.txt");
                        log.log("下载MyHardWare.exe");




                        File.Delete("C:\\ihardware\\client.txt");
                        Thread.Sleep(2000);
                        log.log("删除client.txt");
                        File.Move("C:\\ihardware\\server.txt", "C:\\ihardware\\client.txt");
                        log.log("把server.txt改成client.txt");
                        //pro.WaitForExit();
                    }


                }
                ProcessStartInfo info = new ProcessStartInfo();
                info.FileName = "C:\\ihardware\\MyHardWare.exe";
                info.Arguments = "";
                info.WindowStyle = ProcessWindowStyle.Minimized;
                Process pro = Process.Start(info);
                log.log("运行MyHardWare.exe");

            }

水平有限,高人请路过,谢谢

上一篇:Windows编程系列:WMI


下一篇:监控USB设备插拔