获取文件夹下的所有子文件,读取TXT文档

 DirectoryInfo directory = new DirectoryInfo(path);
                FileInfo[] filelist = directory.GetFiles("*", SearchOption.AllDirectories);
                string informationPath = "";
                for (int i = 0; i < filelist.Length; i++)
                {
                    string fileName = filelist[i].Name;
                    if (Path.GetExtension(fileName)==".txt")
                    {
                        string file=Path.GetFileNameWithoutExtension(fileName);
                        string[] str = file.Split('_');
                        if (str[str.Length - 1] == "result")
                        {
                            informationPath = filelist[i].FullName;
                            break;
                        }
                    }
                }
                StreamReader sr = new StreamReader(informationPath);
                List<string> lines = new List<string>();
                string line = sr.ReadLine();
                //只需要前面三行的信息
                for (int i = 0; i < 3; i++)
                {
                    line.Replace(" ", "");
                    lines.Add(line);
                    line = sr.ReadLine();
                }
                infomation = lines[0] + "\r\n" + lines[1] + "\r\n" + lines[2];
                sr.Close();

猜你喜欢

转载自blog.csdn.net/Sun_xiangyan/article/details/86505500
今日推荐