private List <TagPath> GetTagsFromCsvFile(FileInfo file, string filePath, int versionNo) { var list = new List <TagPath>(); FileStream fs = new FileStream(file.FullName, FileMode.Open); StreamReader sr = new StreamReader(fs, Encoding.Default); //处理第一行 string head = sr.ReadLine(); List <string> headList = ExplainOneLine(head); if (headList[0] != "Name" || headList[1] != "FormatPath") { throw new Exception("Incorrect File format! Please check your .csv File and make sure you have Name and FormatPath in right place"); } //处理其它行 string body; List <string> bodyList; while ((body = sr.ReadLine()) != null) { bodyList = ExplainOneLine(body); TagPath temp = new TagPath() { TagName = bodyList[0], TagFormatPath = bodyList[1], Path = filePath, VersionNo = versionNo }; list.Add(temp); } sr.Close(); fs.Close(); return(list); }
/// <summary> /// 通过配置文件夹路径生成一个TagPathGroup以便后续能存入MongoDB /// </summary> /// <param name="configDirPath">配置文件夹的完整路径,不带结尾分隔符!!!</param> /// <returns></returns> public TagPathGroup GetNewTagPathGroup(string configDirPath, int versionNo) { //因为传过来的可能是相对路径,这样可以获取绝对路径 DirectoryInfo rootDir = new DirectoryInfo(configDirPath); var tagPathGroup = new TagPathGroup(); List <DirectoryInfo> allList = FileDirAssist.GetAllChildDirs(configDirPath); allList.Add(rootDir); List <DirectoryInfo> realList = new List <DirectoryInfo>(); //递归搜索将只添加目录下有.csv的文件夹 foreach (var d in allList) { if (FileDirAssist.IsThisDirHasCsv(d)) { realList.Add(d); } } //添加文件夹节点信息到Collection foreach (var dir in realList) { string path = dir.FullName.Remove(0, rootDir.Parent.FullName.Length); path.Replace(Path.DirectorySeparatorChar, '/'); var temp = new TagPath(); temp.Path = path; temp.VersionNo = versionNo; tagPathGroup.Maps.Add(temp); } var fileAndTagMaps = new List <TagPath>(); //添加每个文件夹下的.csv文件和文件中的Tag信息到tagPathCollection.Maps foreach (var d in tagPathGroup.Maps) { string realPath = d.Path.Replace('/', Path.DirectorySeparatorChar); realPath = realPath.Insert(0, rootDir.Parent.FullName); DirectoryInfo dir = new DirectoryInfo(realPath); foreach (FileInfo f in dir.GetFiles()) { if (f.Extension == ".csv") { //转换文件名格式 string path = f.FullName.Remove(0, rootDir.Parent.FullName.Length); path.Replace(Path.DirectorySeparatorChar, '/'); //添加文件条目 var temp = new TagPath(); temp.Path = path; temp.VersionNo = versionNo; fileAndTagMaps.Add(temp); //添加文件中Tag条目 var tagList = GetTagsFromCsvFile(f, path, versionNo); fileAndTagMaps.AddRange(tagList); } } } tagPathGroup.Maps.AddRange(fileAndTagMaps); CheckIfGroupLegal(tagPathGroup); return(tagPathGroup); }