标签:
公司里每个程序员在命名空间的排序和注释上都有很多的不同。
杂乱的命名空间:
using System; using System.Collections.Generic; using Autodesk.Revit.UI; using BIMCore.UI.ModelessForm; using System.Text; using System.Windows.Forms; using System.Threading; using RevitDocument = Autodesk.Revit.DB.Document; using Autodesk.Revit.DB; using BIMCore.UI; using BIMCore.DB; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using BIMCore.DB.Geometry; using Res = Revit.Addin.isBIM.QuickFilters.Properties.Resources; using BIMCore.DB.Log; namespace Revit.Addin.isBIM.QuickFilters { public partial class CustomForm : System.Windows.Forms.Form { RevitDocument rvtDoc_temp = null; public List<int> Resultlist = null; public List<int> Existinglist = null; ... ....
有序的命名空间:
// // (C) Copyright 2010-2015 by XXX, Inc. // // System namespaces using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Threading; // // Revit namespaces using RevitDocument = Autodesk.Revit.DB.Document; using Autodesk.Revit.DB; // // My namespaces using BIMCore.UI; using BIMCore.DB; using BIMCore.DB.Geometry; using Res = Revit.Addin.isBIM.QuickFilters.Properties.Resources; using BIMCore.DB.Log; using Autodesk.Revit.UI; using BIMCore.UI.ModelessForm; namespace Revit.Addin.isBIM.QuickFilters { public partial class CustomForm : System.Windows.Forms.Form { RevitDocument rvtDoc_temp = null; public List<int> Resultlist = null; public List<int> Existinglist = null; .... .....
为了方便管理代码,这里我制作了一个批量处理.cs文件中命名空间排序及注释的工具。
代码:
private void buttonConfirm_Click(object sender, EventArgs e) { List<string> liststrdocuments = new List<string>(); progressBarFiles.Visible = true; if (!string.IsNullOrWhiteSpace(textBoxFilePath.Text)) { if (!string.IsNullOrWhiteSpace(textBoxNameSpace.Text)) { string[] strdocuments = Directory.GetFiles(strfilepath, "*.cs",SearchOption.AllDirectories); //得到文件夹路径下的所有cs文件路径 if (strdocuments.Length == 0) //判断文件夹中是否没有cs文件 { progressBarFiles.Visible = false; MessageBox.Show(Properties.Resources.StringFileExist); } else { foreach (string strdocu in strdocuments) //排除部分cs文件,因为obj/Debug下的cs文件咱就别管了 { if(boolMode==true) { if (!strdocu.Contains("AssemblyInfo") && !strdocu.Contains("Designer") && !strdocu.Contains("obj") && !strdocu.Contains("designer")) { liststrdocuments.Add(strdocu); } } else { if(!strdocu.Contains("obj")) { liststrdocuments.Add(strdocu); } } } for (int i = 0; i < liststrdocuments.Count; i++) //改变只读属性 { if (File.GetAttributes(liststrdocuments[i]).ToString().IndexOf("ReadOnly") != -1) { File.SetAttributes(liststrdocuments[i], FileAttributes.Normal); } } int intprogress = 0; progressBarFiles.Maximum = liststrdocuments.Count; DataTable dt = new DataTable(); dt.Columns.Add((Properties.Resources.StringDatagridViewCellHeaderOne), typeof(string)); dt.Columns.Add((Properties.Resources.StringDatagridViewCellHeaderTwo), typeof(string)); string strupdatestatus = null; foreach (string Documentpath in liststrdocuments) //遍历每个路径 { intprogress++; progressBarFiles.Value = intprogress; string namespacerest = null; string strusingsystem = null; string strusingAutodesk = null; string strusingBIMCore = null; string strusingrest = null; string namespaceresult = string.Empty; string textboxcopyright = textBoxNameSpace.Text; List<string> listtempline = new List<string>(); List<string> listnamespacerest = new List<string>(); List<string> namespacesurplus = new List<string>(); string[] lines = File.ReadAllLines(Documentpath); //根据路径,分行读取该文件 foreach (string line in lines) { if (line.StartsWith("// System namespaces") || line.StartsWith("//System namespaces") || line.StartsWith("// System Namespaces") || line.StartsWith("//System Namespaces")) { strupdatestatus = Properties.Resources.StringUpdateStatusOne; goto NextDocummentpath; } else { if (line.StartsWith("using")) { listtempline.Add(line); //得到命名空间的行 } else { listnamespacerest.Add(line); //记录剩下的部分 } strupdatestatus = Properties.Resources.StringUpdateStatusTwo; } } #region 对namespace中的多余部分进行处理,比如空格什么的要去掉 foreach (string line in listnamespacerest) { if (line.StartsWith("namespace") || line.StartsWith("[")) { break; } else if(!string.IsNullOrWhiteSpace(line)) { namespacesurplus.Add(line); } } if (namespacesurplus.Count != 0) { for (int i = 0; i < listnamespacerest.Count; i++) { for (int j = 0; j < namespacesurplus.Count; j++) { if (namespacesurplus[j] == listnamespacerest[i]) { listnamespacerest.RemoveAt(i); } } } } foreach (string line in listnamespacerest) { namespacerest += line + "\r\n"; } #endregion listtempline.Sort(); //对命名空间进行排序 foreach (string line in listtempline) //对命名空间行归类 { if (line.StartsWith("using System")) { strusingsystem += line + "\r\n"; } else if (line.StartsWith("using Autodesk")) { strusingAutodesk += line + "\r\n"; } else if (line.StartsWith("using BIMCore")) { strusingBIMCore += line + "\r\n"; } else { strusingrest += line + "\r\n"; } } string strusingAutodeskresult; string strusingBIMCoreresult; string strusingrestresult; strusingAutodeskresult = strusingBIMCoreresult = strusingrestresult = string.Empty; string strusingsystemresult = "// System namespaces" + "\r\n" + strusingsystem + "\r\n"; if (!string.IsNullOrWhiteSpace(strusingAutodesk)) { strusingAutodeskresult = strusingAutodesk; strusingAutodeskresult = "// Autodesk namespaces" + "\r\n" + strusingAutodesk + "\r\n"; } if (!string.IsNullOrWhiteSpace(strusingBIMCore)) { strusingBIMCoreresult = strusingBIMCore; strusingBIMCoreresult = "// BIMCore namespaces" + "\r\n" + strusingBIMCore + "\r\n"; } if (!string.IsNullOrWhiteSpace(strusingrest)) { strusingrestresult = strusingrest; strusingrestresult = "// My namespaces" + "\r\n" + strusingrestresult + "\r\n"; } namespaceresult = textboxcopyright + "\r\n" +strusingsystemresult+ //重写文件 strusingAutodeskresult + strusingBIMCoreresult +strusingrestresult+namespacerest; File.WriteAllText(Documentpath, namespaceresult); textboxcopyright = strusingsystem = strusingAutodesk = strusingBIMCore = strusingrest = namespacerest = string.Empty; //变量清空 NextDocummentpath: //跳转 DataRow dr = dt.NewRow(); // 构建DataTable dr[Properties.Resources.StringDatagridViewCellHeaderOne] = Documentpath; dr[Properties.Resources.StringDatagridViewCellHeaderTwo] = strupdatestatus; dt.Rows.Add(dr); } #region 控件属性的设置 dataGridViewfiles.DataSource = dt; //datagridview的设置 dataGridViewfiles.AllowUserToAddRows = false; dataGridViewfiles.RowHeadersVisible = false; dataGridViewfiles.AllowUserToResizeColumns = false; dataGridViewfiles.AllowUserToResizeRows = false; dataGridViewfiles.Columns[1].Width = Convert.ToInt32(Math.Ceiling(0.3 * Convert.ToDouble(dataGridViewfiles.Width))); //设定更新状态栏的列宽 dataGridViewfiles.Columns[1].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter; //设定更新状态栏的字体居中 buttonConfirm.Top = dataGridViewfiles.Top + dataGridViewfiles.Height + 6; //button位置设置 buttonCancel.Top = dataGridViewfiles.Top + dataGridViewfiles.Height + 6; progressBarFiles.Top = buttonConfirm.Top + buttonConfirm.Height - progressBarFiles.Height-1; //进度条位置设置 progressBarFiles.Visible = false; #endregion } } else { progressBarFiles.Visible = false; MessageBox.Show(Properties.Resources.StringTextBoxCopyrightStauts); } } else { progressBarFiles.Visible = false; MessageBox.Show(Properties.Resources.StringTextBoxFileStatus); } }
我是怎么判断更新的呢?答案是判断cs文件中是否有“// System namespaces”这句话,如果有的话就不需要更新了。如果大家有更棒的方法也能提供给我。
标签:
原文地址:http://www.cnblogs.com/lovecsharp094/p/5427462.html