C#获取指定目录的全部文件信息,包括自动修复各种不规则版本

  /// <summary>
        /// 获取指定目录的文件信息
        /// </summary>
        /// <param name="appRootDirectory">当前根目录</param>
        /// <param name="dirPath"></param>
        /// <param name="fileList"></param>
        public static void GetDirectoryFiles(string appRootDirectory, string dirPath, ref List<UpdateFile> fileList)
        {
            try
            {
                if (!Directory.Exists(dirPath))
                {
                    return;
                }
                DirectoryInfo di = new DirectoryInfo(dirPath);//获取指定文件夹信息
                FileSystemInfo[] fi = di.GetFileSystemInfos();//获取目录下所有文件及文件夹Info(不包含子目录)
                foreach (var i in fi)//遍历每个文件/文件夹
                {
                    if (i is DirectoryInfo)//如果是文件夹
                    {
                        GetDirectoryFiles(appRootDirectory, i.FullName, ref fileList);//调用本身                  
                    }
                    else
                    {
                        FileVersionInfo versionInfo = FileVersionInfo.GetVersionInfo(i.FullName);
                        FileInfo fileInfo = new FileInfo(i.FullName);
                        string strAttributes = fileInfo.Attributes.ToString();
                        if (strAttributes != "Hidden, System, Archive")//系统隐藏的文件
                        {
                            string strVersion = (string.IsNullOrEmpty(versionInfo.FileVersion) ? "1.0.0.0" : versionInfo.FileVersion);
                            string[] verArry = strVersion.Split(new string[] { ".", "," }, StringSplitOptions.RemoveEmptyEntries);//有些是用逗号隔开

                            List<string> verList = new List<string>();
                            foreach (string verItem in verArry)
                            {
                                if (Regex.IsMatch(verItem, @"^[0-9]+$"))
                                {
                                    verList.Add(verItem);
                                }
                                else
                                {
                                    //\myMM\bin\gzip.dll=6.1.2600.0 (xpclient.010817-1148)#2021-12-27 08:44:50
                                    string tempVer = Regex.Match(verItem, @"(<value>^[0-9]+)").Groups["value"].Value;
                                    if (string.IsNullOrEmpty(tempVer))
                                    {
                                        tempVer = "0";
                                    }
                                    verList.Add(tempVer);
                                }

                                if (verList.Count == 4)
                                {
                                    break;
                                }
                            }

                            for (int k = 0; k < 4; k++)
                            {
                                if (verList.Count == 0)
                                {
                                    verList.Add("1");
                                }
                                if (verList.Count <= k)
                                {
                                    verList.Add("0");
                                }
                            }

                            string version = string.Join(".", verList.ToArray());
                            string relativePath = fileInfo.FullName.Replace(appRootDirectory, "");
                            UpdateFile localFile = new UpdateFile(fileInfo.Name, fileInfo.FullName, relativePath, version, fileInfo.Length, fileInfo.LastWriteTime);
                            fileList.Add(localFile);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
            }

        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值