公司里每个程序员在命名空间的排序和注释上都有很多的不同。

杂乱的命名空间:

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-2016 by XXX, Inc.
//
// System namespaces
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; // Autodesk namespaces
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection; // BIMCore namespaces
using BIMCore.DB;
using BIMCore.DB.Geometry; // My namespaces
using Res = Revit.Addin.isBIM.PowerMeasure.Properties.Resources;
using Revit.Addin.isBIM.PowerMeasure.Views;
using Revit.Addin.isBIMAppWrapper; namespace Revit.Addin.isBIM.PowerMeasure
{ ... ...

为了方便管理代码,这里我制作了一个批量处理.cs文件中命名空间排序及注释的工具。

代码:

 private void buttonConfirm_Click(object sender, EventArgs e)
{
string strfilepath = textBoxFilePath.Text;
List<string> liststrdocuments = new List<string>();
progressBarFiles.Visible = true;
if (!string.IsNullOrWhiteSpace(textBoxFilePath.Text) && System.IO.Directory.Exists(textBoxFilePath.Text)) //判断路径是否为空或者是否存在
{
if (!string.IsNullOrWhiteSpace(textBoxNameSpace.Text))
{
string[] strdocuments = Directory.GetFiles(strfilepath, "*.cs",SearchOption.AllDirectories); //得到文件夹路径下的所有cs文件路径
if (strdocuments.Length == ) //判断文件夹中是否没有cs文件
{
progressBarFiles.Visible = false;
MessageBox.Show(Properties.Resources.StringFileExist);
}
else
{
foreach (string strdocu in strdocuments) //排除部分cs文件,其中obj文件夹下的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 = ; i < liststrdocuments.Count; i++) //改变文件只读属性
{
if (File.GetAttributes(liststrdocuments[i]).ToString().IndexOf("ReadOnly") != -)
{
File.SetAttributes(liststrdocuments[i], FileAttributes.Normal);
}
}
int intprogress = ;
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) //遍历每个路径
{
System.Text.Encoding fileEncoding = GetFileEncodeType(Documentpath); //获取该文件的编码格式
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>(); if (DocumentChanged(Documentpath) == false)
{
strupdatestatus = Properties.Resources.StringUpdateStatusOne;
dt=BuildDataTable(dt,Documentpath,strupdatestatus);
}
else
{
string[] lines = File.ReadAllLines(Documentpath); //根据路径,分行读取该文件
foreach (string line in lines)
{
if (line.StartsWith("using"))
{
listtempline.Add(line); //得到命名空间的行
}
else if (!string.IsNullOrWhiteSpace(line))
{
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 != )
{
for (int i = ; i < listnamespacerest.Count; i++)
{
for (int j = ; j < namespacesurplus.Count; j++)
{
if (namespacesurplus[j] == listnamespacerest[i])
{
listnamespacerest.RemoveAt(i);
}
}
}
}
foreach (string line in listnamespacerest)
{
namespacerest += line + "\r\n";
}
#endregion listtempline.Sort(delegate(string str1, string str2) //对命名空间进行排序
{
return Comparer<string>.Default.Compare(str1.Trim(';'), str2.Trim(';'));
}); 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, fileEncoding); textboxcopyright = strusingsystem = strusingAutodesk = strusingBIMCore = strusingrest = namespacerest = string.Empty; //变量清空
dt=BuildDataTable(dt, Documentpath, strupdatestatus); } } #region 控件属性的设置
dataGridViewfiles.DataSource = dt; //datagridview的设置
dataGridViewfiles.AllowUserToAddRows = false;
dataGridViewfiles.RowHeadersVisible = false;
dataGridViewfiles.AllowUserToResizeColumns = false;
dataGridViewfiles.AllowUserToResizeRows = false;
dataGridViewfiles.Columns[].Width = Convert.ToInt32(Math.Ceiling(0.3 * Convert.ToDouble(dataGridViewfiles.Width))); //设定更新状态栏的列宽
dataGridViewfiles.Columns[].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter; //设定更新状态栏的字体居中
progressBarFiles.Visible = false;
#endregion
}
}
else
{
progressBarFiles.Visible = false;
MessageBox.Show(Properties.Resources.StringTextBoxCopyrightStauts);
}
}
else
{
progressBarFiles.Visible = false;
MessageBox.Show(Properties.Resources.StringTextBoxFileStatus);
}
}

169行对文件重写时依然使用文件原有编码格式,防止打开文件时候有乱码。
52行的子函数 GetFileEncodeType(string filename)判断编码格式    函数转载地址:http://www.cnblogs.com/swtseaman/archive/2011/05/17/2048689.html

 public System.Text.Encoding GetFileEncodeType(string filename)
{
System.IO.FileStream fs = new System.IO.FileStream(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read, FileShare.ReadWrite);
//FileShare.ReadWrite, 不然文件在进行其他IO操作时进程会被占用而报错 System.IO.BinaryReader br = new System.IO.BinaryReader(fs);
Byte[] buffer = br.ReadBytes();
if(buffer[]>=0xEF)
{
if(buffer[]==0xEF && buffer[]==0xBB)
{
return System.Text.Encoding.UTF8;
}
else if(buffer[]==0xFE && buffer[]==0xFF)
{
return System.Text.Encoding.BigEndianUnicode;
}
else if(buffer[]==0xFF && buffer[]==0xFE)
{
return System.Text.Encoding.Unicode;
}
else
{
return System.Text.Encoding.Default;
}
}
else
{
return System.Text.Encoding.Default;
}
}
#endregion

68行为文件更新判据:判断cs文件中是否有“// System namespaces”, 还有就是cs文件中的copyright部分是否与winform中的copyright文本框内容相同。

 private bool DocumentChanged(string path)
{
bool boolWholeStatus = true;
bool boolStatus1 = false;
bool boolStatus2 = false;
string oralcopyright = string.Empty;
string txtnamspace = textBoxNameSpace.Text+"\r\n";
string[] lines = File.ReadAllLines(path); //根据路径,分行读取该文件 foreach (string line in lines)
{
if (line.StartsWith("// System namespaces") || line.StartsWith("// System Namespaces") || line.StartsWith("//System namespaces") ||
line.StartsWith("//System Namespaces"))
{
boolStatus2 = true;
break;
}
else if(!string.IsNullOrEmpty("line"))
{
oralcopyright += line + "\r\n";
}
}
if (txtnamspace.Equals(oralcopyright))
{
boolStatus1 = true;
} if (boolStatus1 == true && boolStatus2 == true)
{
boolWholeStatus = false;
}
return boolWholeStatus;
}

71行的datatable构建方法:

private DataTable BuildDataTable(DataTable dt, string path, string status)
{
DataRow dr = dt.NewRow();
dr[Properties.Resources.StringDatagridViewCellHeaderOne] = path;
dr[Properties.Resources.StringDatagridViewCellHeaderTwo] = status;
dt.Rows.Add(dr);
return dt;
}

【Winform】.cs文件命名空间排序及注释批量处理工具的更多相关文章

  1. 基于Winform的.cs文件命名空间排序及注释批量处理工具

    公司里每个程序员在命名空间的排序和注释上都有很多的不同. 杂乱的命名空间: using System; using System.Collections.Generic; using Autodesk ...

  2. 百度翻译cs文件英文注释

    原由:本人英语烂,没办法看不懂国外的代码注释!只能借助其他手段来助我一臂之力了. 虽然翻译内容不是很准确,但好过什么都看不懂的强. 对吧?! 代码有点乱有用的园友自个整理一下吧! 最近没时间所以翻译后 ...

  3. WinForm中AssemblyInfo.cs文件参数具体讲解

    在.NET中有一个配置文件AssemblyInfo.cs主要用来设定生成的有关程序集的常规信息dll文件的一些参数,下面是默认的AssemblyInfo.cs文件的内容具体介绍 //是否符合公共语言规 ...

  4. CS文件类头注释

    1.修改unity生成CS文件的模板(模板位置:Unity\Editor\Data\Resources\ScriptTemplates 文件名:81-C# Script-NewBehaviourScr ...

  5. 给VS类文件添加默认头注释

    找到类文件所在路径:C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\ItemTemplatesCache\CSharp\ ...

  6. 从java文件和CS文件里查询方法使用次数工具

    前几天,领导让我找一下老系统(Java)里getRemoteUser方法都哪个文件用了,package是什么,方法被调用了多少次,当时因为着急,所以,直接人工找的,但是以后要是再出现,人工找就太讨厌了 ...

  7. WPF根据Oracle数据库的表,生成CS文件小工具

    开发小工具的原因: 1.我们公司的开发是客户端用C#,服务端用Java,前后台在通讯交互的时候,会用到Oracle数据库的字段,因为服务器端有公司总经理开发的一个根据Oracle数据库的表生成的cla ...

  8. C# 调用WebService的3种方式 :直接调用、根据wsdl生成webservice的.cs文件及生成dll调用、动态调用

    1.直接调用 已知webservice路径,则可以直接 添加服务引用--高级--添加web引用 直接输入webservice URL.这个比较常见也很简单 即有完整的webservice文件目录如下图 ...

  9. 根据wsdl文件生成webservice 的.cs文件 及 生成dll C#调用

               Visual Studio 2013->Visual Studio Tools->VS2013 开发人员命令提示 命令行输入 wsdl E:\WS.wsdl /out ...

随机推荐

  1. 关于Python的多重排序

    Python预置的list.sort().sorted()方法可实现各种数组的排序,但支持的只限于一个key,如果要多重排序,目前所知的方法只有自定义了. Help on built-in funct ...

  2. 02-JVM内存模型:虚拟机栈与本地方法栈

    一.虚拟机栈(VM Stack) 1.1)什么是虚拟机栈 虚拟机栈是用于描述java方法执行的内存模型. 每个java方法在执行时,会创建一个“栈帧(stack frame)”,栈帧的结构分为“局部变 ...

  3. jmeter 函数助手

    1.选项,函数助手对话框,打开函数助手 2.使用方法 输入参数,点击生成,可以直接使用(Name of variable in which to store the result (optional) ...

  4. Linux命令应用大词典-第26章 模块和内核管理

    26.1 lsmod:显示内核中模块的状态 26.2 get_module:查看内核模块详细信息 26.3 modinfo:显示内核模块信息

  5. Python 函数参数类型大全(非常全!!!)

    Python 函数参数类型大全(非常全!!!) 1.在python编写程序里面具有函数文档,它的主要作用是为了让别人可以更好的理解你的函数,所以这是一个好习惯,访问函数文档的方式是: MyFuncti ...

  6. js写的数码时钟,在“最小化”浏览器 或者 “切换网页”是动画效果好像不对

    一.问题 在“最小化”浏览器 或者 “切换网页”是动画效果不对,不知道哪里出了问题???是不是”最小化“时网页定时器关掉了,还是其他什么原因啊 ???? 二.HTML代码如下 <div id=& ...

  7. vue中如何实现pdf文件预览?

    今天产品提出一个优化的需求,就是之前我们做的图片展示就是一个img标签搞定,由于我们做的是海外后台管理系统,那边的人上传的文件时pdf格式,vue本事是不支持这种格式文件展示的,于是就google搜索 ...

  8. some Commands OF CONSOLE

    不可避免地使用console,一旦与电脑打交道:入口就是help,而很多行就直接过掉了,却不能看到需要的地方,在那里停下来,实际是需要使用more  less grep等 在windows中,使用di ...

  9. LeetCode 96——不同的二叉搜索树

    1. 题目 2. 解答 以 \(1, 2, \cdots, n\) 构建二叉搜索树,其中,任意数字都可以作为根节点来构建二叉搜索树.当我们将某一个数字作为根节点后,其左边数据将构建为左子树,右边数据将 ...

  10. HADOOP/HDFS Essay

    HDFS架构 the core of HADOOP/distributed systems is storeage(HDFS) and resource manager(YARN) for compu ...