/// <summary> /// 截取CS文件所需的内容 /// </summary> /// <param name="directoryInfo"></param> /// <param name="projectCode">项目代码</param> /// <param name="entityPath">路径</param> /// <param name="name">文件名称</param> public void GetDetailList(DirectoryInfo directoryInfo, string projectCode, string entityPath, string name) { var entityFileList = directoryInfo.GetFiles("*" + suffix); if (entityFileList.Count() > 0) { foreach (FileInfo files in entityFileList) { EntityInfoDTO entityInfo = new EntityInfoDTO(); entityInfo.ProjectCode = projectCode.Trim(); entityInfo.FileNames = name.Trim(); //获取实体名 entity = files.ToString(); entity = entity.Replace(suffix, ""); //判断文件是否存在 if (File.Exists(files.FullName)) { var summary = ""; var entityField = ""; var types = ""; var tableField = ""; try { #region 截取操作 using (StreamReader sr = File.OpenText(files.FullName)) { string str = File.ReadAllText(files.FullName); string[] strArray = str.Split(new char[1] { ':', }); var entityArray = strArray[0].Replace("\r\n", ""); string[] entityStrArray = Regex.Split(entityArray, "class", RegexOptions.IgnoreCase); string[] entitySummary = Regex.Split(entityStrArray[0], "///", RegexOptions.IgnoreCase); entityInfo.Summary = entitySummary[2] == null ? "命名不规范" : entitySummary[2].Trim(); entityField = entityStrArray[1]; entityInfo.EntityName = entityField.Trim(); entityList.Add(entityInfo); var last = strArray[1].Replace("\r\n", ""); string[] strArray1 = last.Split(new char[1] { '{' }); for (int x = 0; x < strArray1.Length; x++) { var st = strArray1[x].Replace("\r\n", ""); if (strArray1[x].Contains("///")) { string[] strArray2 = Regex.Split(st, "///", RegexOptions.IgnoreCase); if (!strArray2[0].Contains("Entity")) { summary = strArray2[2]; var st2 = strArray2[3].Replace("\r\n", ""); string[] strArray3 = Regex.Split(st2, "public", RegexOptions.IgnoreCase); var st3 = strArray3[1].Replace("\r\n", ""); if (strArray3[1].Contains(" ?")) { st3 = st3.Replace(" ?", "?"); } else if (strArray3[1].Contains(" ")) { st3 = st3.Replace(" ", " "); } string[] strArray4 = Regex.Split(st3, " ", RegexOptions.IgnoreCase); types = strArray4[1]; tableField = strArray4[2]; var detailDto = new DataDetailDictionaryDTO(); detailDto.EntityPath = entityPath; detailDto.ProjectCode = projectCode; detailDto.EntityName = (entity.Length == 0) ? detailDto.EntityName = "命名不规范" : detailDto.EntityName = entity; detailDto.TableField = (tableField.Length == 0) ? detailDto.TableField = "命名不规范" : detailDto.TableField = tableField; detailDto.Type = (types.Length == 0) ? detailDto.Type = "命名不规范" : detailDto.Type = types; detailDto.Summary = (summary.Length == 0) ? detailDto.Summary = "命名不规范" : detailDto.Summary = summary; detailList.Add(detailDto); } } else if (st.Contains("public")) { string[] strArray3 = Regex.Split(st, "public", RegexOptions.IgnoreCase); var st3 = strArray3[1].Replace("\r\n", ""); if (strArray3[1].Contains(" ?")) { st3 = st3.Replace(" ?", "?"); } else if (strArray3[1].Contains(" ")) { st3 = st3.Replace(" ", " "); } string[] strArray4 = Regex.Split(st3, " ", RegexOptions.IgnoreCase); types = strArray4[1]; tableField = strArray4[2]; var detailDto = new DataDetailDictionaryDTO(); detailDto.EntityPath = entityPath; detailDto.ProjectCode = projectCode; detailDto.EntityName = (entity.Length == 0) ? detailDto.EntityName = "命名不规范" : detailDto.EntityName = entity; detailDto.TableField = (tableField.Length == 0) ? detailDto.TableField = "命名不规范" : detailDto.TableField = tableField; detailDto.Type = (types.Length == 0) ? detailDto.Type = "命名不规范" : detailDto.Type = types; detailDto.Summary = (summary.Length == 0) ? detailDto.Summary = "命名不规范" : detailDto.Summary = summary; detailList.Add(detailDto); } } } #endregion } catch { } } } } }
public static void GetDetailFilesList(string entityPath, string projectCode) { string suffix = ".cs"; string type = " "; string entity = ""; DirectoryInfo inputfolder = new DirectoryInfo(entityPath); //获取所有文件夹 var list = inputfolder.GetDirectories(); var inputFileList = list.Where(a => a.Name != "Properties").ToArray(); if (inputFileList.Count() > 0) { //获取表头类型 for (int i = 0; i < inputFileList.Count(); i++) { type = inputFileList[i].ToString(); //Console.WriteLine("文件夹名称:" + type); DirectoryInfo entityfolder = new DirectoryInfo(entityPath + "\\" + type); //获取所有的cs文件 var entityFileList = entityfolder.GetFiles("*" + suffix); if (entityFileList.Count() > 0) { foreach (FileInfo files in entityFileList) { //获取实体名 entity = files.ToString(); entity = entity.Replace(suffix, ""); //Console.WriteLine("实体名:" + entity); //判断文件是否存在 if (File.Exists(files.FullName)) { var summary = ""; var entityField = ""; var types = ""; var tableField = ""; try { using (StreamReader sr = File.OpenText(files.FullName)) { string str = File.ReadAllText(files.FullName); // Console.WriteLine(str); string[] strArray = str.Split(new char[1] { ':', }); var entityArray = strArray[0].Replace("\r\n", ""); string[] entityStrArray = Regex.Split(entityArray, "class", RegexOptions.IgnoreCase); entityField = entityStrArray[1]; Console.WriteLine("实体名:" + entityField); var last = strArray[1].Replace("\r\n", ""); string[] strArray1 = last.Split(new char[1] { '{' }); for (int x = 0; x < strArray1.Length; x++) { var st = strArray1[x].Replace("\r\n", ""); Console.WriteLine(strArray1[x]); if (strArray1[x].Contains("///")) { string[] strArray2 = Regex.Split(st, "///", RegexOptions.IgnoreCase); Console.WriteLine("test:" + strArray2[2]); if (!strArray2[0].Contains("Entity")) { //获取Summary strArray2[2] summary = strArray2[2]; Console.WriteLine("注释名:" + summary); //Console.WriteLine("strArray2[3]:" + strArray2[3]); var st2 = strArray2[3].Replace("\r\n", ""); string[] strArray3 = Regex.Split(st2, "public", RegexOptions.IgnoreCase); //Console.WriteLine("strArray3[3]:" + strArray3[1]); var st3 = strArray3[1].Replace("\r\n", ""); if (strArray3[1].Contains(" ?")) { st3 = st3.Replace(" ?", "?"); Console.WriteLine("strArray4[1]:" + st3); } else if (strArray3[1].Contains(" ")) { st3 = st3.Replace(" ", " "); Console.WriteLine("strArray4[1]:" + st3); } string[] strArray4 = Regex.Split(st3, " ", RegexOptions.IgnoreCase); types = strArray4[1]; //Console.WriteLine("类型:" + types); tableField = strArray4[2]; // Console.WriteLine("字段:" + tableField); var detailDto = new DataDetailDictionaryDTO(); detailDto.EntityPath = entityPath; detailDto.ProjectCode = projectCode; detailDto.EntityName = (entity == null) ? detailDto.EntityName = "命名不规范" : detailDto.EntityName = entity; detailDto.TableField = (tableField == null) ? detailDto.TableField = "命名不规范" : detailDto.TableField = tableField; detailDto.Type = (types == null) ? detailDto.Type = "命名不规范" : detailDto.Type = types; Console.WriteLine("类型1:" + types); Console.WriteLine("类型2:" + detailDto.Type); detailDto.Summary = (summary == null) ? detailDto.Summary = "命名不规范" : detailDto.Summary = summary; detailList.Add(detailDto); } } //else //{ // summary = "命名不规范"; // string[] strArray3 = Regex.Split(st, "public", RegexOptions.IgnoreCase); // Console.WriteLine("strArray3[1]:" + strArray3[1]); // var st3 = strArray3[1].Replace("\r\n", ""); // if (strArray3[1].Contains(" ?")) // { // st3 = st3.Replace(" ?", "?"); // //Console.WriteLine("strArray4[1]:" + st3); // } // else if (strArray3[1].Contains(" ")) // { // st3 = st3.Replace(" ", " "); // //Console.WriteLine("strArray4[1]:" + st3); // } // string[] strArray4 = Regex.Split(st3, " ", RegexOptions.IgnoreCase); // types = strArray4[1]; // //Console.WriteLine("类型:" + types); // tableField = strArray4[2]; // //Console.WriteLine("字段:" + tableField); // var detailDto = new DataDetailDictionaryDTO(); // detailDto.EntityPath = entityPath; // detailDto.ProjectCode = projectCode; // detailDto.EntityName = (entity == null) ? detailDto.EntityName = "命名不规范" : detailDto.EntityName = entity; // detailDto.TableField = (tableField == null) ? detailDto.TableField = "命名不规范" : detailDto.TableField = tableField; // detailDto.Type = (types == null) ? detailDto.Type = "命名不规范" : detailDto.Type = types; // detailDto.Summary = (summary == null) ? detailDto.Summary = "命名不规范" : detailDto.Summary = summary; // detailList.Add(detailDto); //} } } } catch { } } } } } } SaveDetailToExcelFile(detailList, DefaultPath(@"C:\DDExcelTest") + @"\" + projectCode + "_DataDetailDictionary"); Console.WriteLine("运行完毕"); Console.ReadKey(); }