smple

//遍历文件
        static List<string> TraversalFile(string filePath,string fileExtension)
        {
            List<string> returnList = new List<string>();
            if (Directory.Exists(filePath) == true)
            {
                DirectoryInfo theFolder = new DirectoryInfo(filePath);
                var fileItem = from file in theFolder.GetFiles() where file.Extension == fileExtension select file;
                foreach (var item in fileItem)
                    returnList.Add(item.Name.Replace(item.Extension, string.Empty));
            }
            return returnList;
        }

//读XML
        static internal void Read()
        {
            var path = @"d:\Environment.xml";
            IDictionary<string, string> aa = new Dictionary<string,string>();
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(path);
                var nodeList = doc.SelectSingleNode("config").ChildNodes;
                foreach (XmlNode node in nodeList)
                {
                    aa.Add(node.Name, node.InnerText);
                }
            }
            catch
            {
            }
        }

    /// <summary>
        /// 匹配
        /// </summary>
        /// <param name="targetString"></param>
        /// <returns></returns>
        private int regexForInt(string targetString)
        {
            int result = 0;

            if (String.IsNullOrEmpty(targetString))
            {
                return result;
            }
            else {
                Regex intRule = new Regex(@"[0-9]+$");//只匹配结尾的数字,其他部分的数字会被忽略
                if (intRule.Match(targetString, 0).Value!=string.Empty)
                    result = int.Parse(intRule.Match(targetString, 0).Value);
                else
                {
                    result = 0;
                }

               
                //方法2:所有的数字都被选出来按照左至右顺序排列
                //Regex intRule = new Regex(@"\d+");
                //string tmp = "";
                //foreach (Match loopRecord in Regex.Matches(targetString, @"\d+"))
                //{
                //    tmp += loopRecord.Value.ToString();
                //}
                //result = Convert.ToInt32(tmp);
                return result;
            }

        }

原文地址:https://www.cnblogs.com/renfeng/p/2506352.html