前天转了一篇:如何将自己的代码自动添加版权信息的及其扩展[转],便忙忙活活地去更改项目模板,一打开文件夹,傻眼了,这么多文件,手动添加得费多长时间,亏是做开发的,便手动写了一个更改模板的小程序,现贴出代码:
// 注释信息数组
private string[] _comment = new string[15];
// 最终重新写入的行数组
private string[] destLines;
// 储存文件中符合条件的行
private List lLines = new List();
// 统计执行文件的个数
private int _fileCount = 0;
前天转了一篇:如何将自己的代码自动添加版权信息的及其扩展[转],便忙忙活活地去更改项目模板,一打开文件夹,傻眼了,这么多文件,手动添加得费多长时间,亏是做开发的,便手动写了一个更改模板的小程序,现贴出代码:
// 注释信息数组
private string[] _comment = new string[15];
// 最终重新写入的行数组
private string[] destLines;
// 储存文件中符合条件的行
private List<string> lLines = new List<string>();
// 统计执行文件的个数
private int _fileCount = 0;
// 模板所在目录
private string _projectPath = @"C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ProjectTemplatesCache";
private string _itemPath = @"C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplatesCache";
在窗体Load事件中初使化所需的数据:
private void FrmCSComment_Load(object sender, EventArgs e)
{
// 禁止捕获错误线程调用
CheckForIllegalCrossThreadCalls = false;
_comment[0] = "#region Copyright (c) 2009-2010 upupto . All rights reserved";
_comment[1] = "// ************************************************************";
_comment[2] = "// Copyright (c) 2009-2010 upupto . All rights reserved";
_comment[3] = "// File name: $safeitemname$.cs";
_comment[4] = "// CLR version: $clrversion$";
_comment[5] = "// Author: upupto";
_comment[6] = "// Blog: http://www.cnblogs.com/upupto/";
_comment[7] = "// Version: 1.0.0";
_comment[8] = "// Date: $time$";
_comment[9] = "// Description: ";
_comment[10] = "// ";
_comment[11] = "// History: ";
_comment[12] = "// ************************************************************";
_comment[13] = "#endregion Copyright (c) 2009-2010 upupto . All rights reserved";
_comment[14] = "";
// 开始执行后台操作
backgroundWorker1.RunWorkerAsync();
}
接下来就是后台的操作了:
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
lblDir.Text = "获取文件夹权限";
DirectoryInfo _projectDir = new DirectoryInfo(_projectPath);
DirectorySecurity dSecurity = _projectDir.GetAccessControl();
dSecurity.RemoveAccessRule(new FileSystemAccessRule(Environment.UserName, FileSystemRights.FullControl, AccessControlType.Allow));
_projectDir.SetAccessControl(dSecurity);
DirectoryInfo _itemDir = new DirectoryInfo(_itemPath);
dSecurity = _itemDir.GetAccessControl();
dSecurity.RemoveAccessRule(new FileSystemAccessRule(Environment.UserName, FileSystemRights.FullControl, AccessControlType.Allow));
_itemDir.SetAccessControl(dSecurity);ForeachDir(_projectDir);
ForeachDir(_itemDir);
}
private void ForeachDir(DirectoryInfo _dir)
{
lblDir.Text = _dir.FullName;
foreach (FileInfo file in _dir.GetFiles("*.cs"))
{
AppendComment(file.FullName);
_fileCount++;
listBox1.Items.Add(file.FullName);
listBox1.SelectedIndex = listBox1.Items.Count - 1;
Thread.Sleep(20);
}
foreach (DirectoryInfo dInfo in _dir.GetDirectories())
{
ForeachDir(dInfo); // 递归调用
}
}
/// <summary>
/// 添加文件注释
/// </summary>
/// <param name="srcFile">目标文件</param>
private void AppendComment(string srcFile)
{
if (File.Exists(srcFile))
{
string[] lines = File.ReadAllLines(srcFile);
int i = 0;
if (lines.Length > 0)
{
try
{
for (i = 0; i < lines.Length; i++)
{
if (!lines[i].Equals("") && lines[i].Contains("using "))
{
break;
}
}
for (int j = i; j < lines.Length; j++)
{
lLines.Add(lines[j]);
}
destLines = new string[lLines.Count + _comment.Length + 1];
Array.Copy(_comment, 0, destLines, 0, _comment.Length);
Array.Copy(lLines.ToArray(), 0, destLines, _comment.Length, lLines.Count);
File.WriteAllLines(srcFile, destLines);
}
catch { }
finally
{
Array.Clear(destLines, 0, destLines.Length);
lLines.Clear();
GC.CollectionCount(0);
}
}
Array.Clear(lines, 0, lines.Length);
}
}
操作完成后,显示完成信息:
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
lblDir.Text = "执行完毕,共计:" + _fileCount + " 文件";
}