Loading
0

c#文件夹以及子文件夹遍历

技术小学生微信公众号
腾讯云服务器大促销。
华为服务器
前言:遍历文件夹及遍历子文件夹可参考如下操作

public class DirectoryAllFiles
        {
            static List<FileInformation> FileList = new List<FileInformation>();
            public static List<FileInformation> GetAllFiles(DirectoryInfo dir)
            {
                FileInfo[] allFile = dir.GetFiles();
                foreach (FileInfo fi in allFile)
                {
                    FileList.Add(new FileInformation { FileName = fi.Name, FilePath = fi.FullName });
                }
                DirectoryInfo[] allDir = dir.GetDirectories();
                foreach (DirectoryInfo d in allDir)
                {
                    GetAllFiles(d);
                }
                return FileList;
            }
        }

        public class FileInformation
        {
            public string FileName { get; set; }
            public string FilePath { get; set; }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //刷新
            //读取所有目录
            //自动更新程序列表
            string dir = Application.StartupPath;
            List<FileInformation> list = DirectoryAllFiles.GetAllFiles(new System.IO.DirectoryInfo(dir));
            if (list.Where(t => t.FileName.ToLower().Contains("android")).Any())
            Console.WriteLine("true");
            foreach (var item in list)
            {
                //Console.WriteLine(string.Format("文件名:{0}---文件目录{1}", item.FileName, item.FilePath));
                if (item.FileName.Contains("1.exe"))
                {
                    ListViewItem lvitem = new ListViewItem();
                    lvitem.SubItems[0].Text = item.FileName;
                    lvitem.SubItems.Add(item.FilePath); //strs[1] 目录
                    listView1.Items.Add(lvitem);
                }
            }
        }

技术小学生微信公众号
华为服务器
腾讯云服务器大促销。

声明:站长码字很辛苦啊,转载时请保留本声明及附带文章链接:https://blog.tag.gg/showinfo-23-36054-0.html
亲爱的:若该文章解决了您的问题,可否收藏+评论+分享呢?
上一篇:C# 实现弹出多个选择窗体窗口并传回值
下一篇:c#加密产生随机数方法,加密生成器随机生成