C#WinFrom查找盘内文件的信息(获取该电脑的IP地址,MAC地址,电脑名,搜索文件的文件版本信息)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Diagnostics;

using System.Management;

--引用

--文件路径是个ListBox

--文件名是个ListBox

private void button1_Click(object sender, EventArgs e)

        {
            this.文件路径.Items.Add("C:\\");
            this.文件路径.Items.Add("D:\\");
            this.文件路径.Items.Add("E:\\");

            this.文件路径.Items.Add("F:\\");

        --添加搜索的盘符或者文件夹路径

        ---如果要搜索当前Exe 所在文件夹可以

--string strFullPath = Application.StartupPath;
  --      this.文件路径.Items.Add(strFullPath);

            labResult.Text="搜索中";
            for (int i = 0; i < 文件路径.Items.Count; i++)
            {




                DirectoryInfo TheFolder = new DirectoryInfo(文件路径.Items[i].ToString());


                try
                {
                    foreach (DirectoryInfo NextFolder in TheFolder.GetDirectories())
                    {
                        try
                        {
                            this.文件路径.Items.Add(NextFolder.FullName);


                        }
                        catch (Exception)
                        {




                        }


                    }
                }
                catch (Exception)
                {




                }
                //遍历文件夹


                //遍历文件
                try
                {
                    foreach (FileInfo NextFile in TheFolder.GetFiles())
                    {
                        try
                        {

                            int z;

                          ---  要搜索文件的文件名

--           要查找包含的文件也可以这样写

--if (NextFile.Name.ToString().Contains(".log"))

                            if (NextFile.Name.ToString() == "DataProcess.dll")
                            {
                                System.IO.FileInfo file = new System.IO.FileInfo(NextFile.FullName);
                                if (file.Exists)

                                {

 string ip = Dns.GetHostEntry(Dns.GetHostName()).AddressList.FirstOrDefault<IPAddress>(a => a.AddressFamily.ToString().Equals("InterNetwork")).ToString();

--获取当前电脑的IP地址

                                    FileVersionInfo ver = FileVersionInfo.GetVersionInfo(file.ToString());

--获取当前文件的文件版本

                                    string dllVersion = ver.FileVersion;
                                    string mac = null;

                                    ManagementObjectSearcher query = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration");

--获取文件的Mac地址

                                    ManagementObjectCollection queryCollection = query.Get();
                                    foreach (ManagementObject mo in queryCollection)
                                    {
                                        if (mo["IPEnabled"].ToString() == "True")
                                            mac = mo["MacAddress"].ToString();

                                    }

--获取当前电脑的电脑名

                                    string cname = System.Environment.MachineName;
                                 --插入数据库   

z = SqlDesigner.ExecuteNoQuery("insert into uploadver(path,filename,ip,ver,mac,MachineName)values('" + file.ToString() + "','" + file.Name + "','" + ip + "','" + dllVersion + "','" + mac + "','" + cname + "') ");

    --插入一笔弹一次

                                    if (z > 0)
                                    {
                                        MessageBox.Show("上传成功" + z + "条");


                                    }
                                    else
                                    {
                                        MessageBox.Show("上传失败");
                                    }
                                    
                                }


                                
                                


                            }

                        --把找到的文件名插入到Listbox
                            this.文件名.Items.Add(NextFile.Name);


                        }
                        catch (Exception)
                        {




                        }


                    }
                }
                catch (Exception)
                {




                }


            }


          
            labResult.Text = "搜索结束";


        }
        }
发布了17 篇原创文章 · 获赞 4 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/ruo40018293/article/details/80234486