基本信息
源码名称:VS批注批量修改CommentHelper
源码大小:0.27M
文件格式:.rar
开发语言:C#
更新时间:2015-09-23
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Security.AccessControl;
using System.Threading;
using System.Runtime.InteropServices;
namespace CommentHelper
{
public partial class MainForm : Form
{
private String[] targetPaths;
private String[] comment;
private String[] commentStart = new String[] { "#region CopyRight", "/**************************************************************" };
private String[] commentEnd = new String[] { " **************************************************************/", "#endregion CopyRight", "" };
private bool backUp = false;
BackgroundWorker backgroundWorker;
public MainForm()
{
if (Environment.Is64BitOperatingSystem)
{
targetPaths = new String[] {
@"C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\ProjectTemplatesCache"
,@"C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplatesCache"
,@"C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ProjectTemplatesCache"
,@"C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplatesCache"
,@"C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\ProjectTemplatesCache"
,@"C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\ItemTemplatesCache"
,@"C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\ProjectTemplatesCache"
,@"C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\ItemTemplatesCache"
};
}
else
{
targetPaths = new String[] {
@"C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ProjectTemplatesCache"
,@"C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplatesCache"
,@"C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\ProjectTemplatesCache"
,@"C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplatesCache"
,@"C:\Program Files\Microsoft Visual Studio 11.0\Common7\IDE\ProjectTemplatesCache"
,@"C:\Program Files\Microsoft Visual Studio 11.0\Common7\IDE\ItemTemplatesCache"
,@"C:\Program Files\Microsoft Visual Studio 12.0\Common7\IDE\ProjectTemplatesCache"
,@"C:\Program Files\Microsoft Visual Studio 12.0\Common7\IDE\ItemTemplatesCache"
};
}
InitializeComponent();
backgroundWorker = new BackgroundWorker();
backgroundWorker.DoWork = new DoWorkEventHandler(backgroundWorker_DoWork);
backgroundWorker.RunWorkerCompleted = new RunWorkerCompletedEventHandler(backgroundWorker_RunWorkerCompleted);
}
void backgroundWorker_RunWorkerCompleted(Object sender, RunWorkerCompletedEventArgs e)
{
if (e.Error != null)
{
MessageBox.Show(e.Error.ToString(), "错误");
SetStatusText(e.Error.Message);
}
else
{
SetStatusText("处理完成!");
}
}
private void btnApply_Click(Object sender, EventArgs e)
{
if (String.IsNullOrEmpty(this.tbComment.Text.Trim()))
{
MessageBox.Show("输入备注!", "提示");
return;
}
if (!backgroundWorker.IsBusy)
{
comment = this.tbComment.Text.Replace("\r\n", "\n").Split('\n');
backUp = this.cbBackUp.Checked;
backgroundWorker.RunWorkerAsync();
}
else
{
MessageBox.Show("正在进行处理,请勿重复点击!", "提示");
}
}
void backgroundWorker_DoWork(Object sender, DoWorkEventArgs e)
{
Process();
}
private void Process()
{
SetStatusText("开始处理...");
String backupDir = String.Format("D:\\CommentHelper\\Backup_{0:yyyyMMddHHmmss}\\", DateTime.Now);
foreach (var item in targetPaths)
{
if (Directory.Exists(item))
{
DirectoryInfo directoryInfo = new DirectoryInfo(item);
if (backUp)
{
SetStatusText("正在备份:...\\" directoryInfo.Name);
//CopyDirectory(item, Path.Combine(backupDir, directoryInfo.Name));
if (item.Contains("9.0"))
{
CopyDirectory(item, backupDir "\\VS2008\\" directoryInfo.Name);
}
else if (item.Contains("10.0"))
{
CopyDirectory(item, backupDir "\\VS2010\\" directoryInfo.Name);
}
else if (item.Contains("11.0"))
{
CopyDirectory(item, backupDir "\\VS2012\\" directoryInfo.Name);
}
else if (item.Contains("12.0"))
{
CopyDirectory(item, backupDir "\\VS2013\\" directoryInfo.Name);
}
}
DirectorySecurity dSecurity = directoryInfo.GetAccessControl();
dSecurity.RemoveAccessRule(new FileSystemAccessRule(Environment.UserName, FileSystemRights.FullControl, AccessControlType.Allow));
directoryInfo.SetAccessControl(dSecurity);
ProcessDirectory(directoryInfo);
}
}
}
private void ProcessDirectory(DirectoryInfo dir)
{
SetStatusText("处理文件夹:" GetShortPathName(dir.FullName));
foreach (FileInfo file in dir.GetFiles("*.cs"))
{
ProcessFile(file.FullName);
Thread.Sleep(20);
}
foreach (DirectoryInfo child in dir.GetDirectories())
{
ProcessDirectory(child);
}
}
private void ProcessFile(String filePath)
{
SetStatusText("处理文件:" GetShortPathName(filePath));
if (File.Exists(filePath))
{
String[] oldLines = File.ReadAllLines(filePath);
List<String> lineList = new List<String>();
String[] newLines;
int i = 0;
if (oldLines.Length > 0)
{
try
{
for (i = 0; i < oldLines.Length; i )
{
if (oldLines[i].Contains("#region CopyRight"))
{
i ;
while (!oldLines[i].Contains("#endregion CopyRight"))
{
i ;
}
i = 2;
}
lineList.Add(oldLines[i]);
}
for (; ; )
{
if (String.IsNullOrWhiteSpace(lineList[lineList.Count - 1]))
{
lineList.RemoveAt(lineList.Count - 1);
}
else
{
break;
}
}
newLines = new String[lineList.Count commentStart.Length comment.Length commentEnd.Length];
Array.Copy(commentStart, 0, newLines, 0, commentStart.Length);
Array.Copy(comment, 0, newLines, commentStart.Length, comment.Length);
Array.Copy(commentEnd, 0, newLines, commentStart.Length comment.Length, commentEnd.Length);
Array.Copy(lineList.ToArray(), 0, newLines, commentStart.Length comment.Length commentEnd.Length, lineList.Count);
File.WriteAllLines(filePath, newLines);
}
catch { }
finally
{
GC.CollectionCount(0);
}
}
}
}
private delegate void SetStatusTextDelegate(String text);
private void SetStatusText(String text)
{
SetStatusTextDelegate proxy = SetStatusTextInvoke;
this.Invoke(proxy, text);
}
private void SetStatusTextInvoke(String text)
{
this.toolStripStatusLabel1.Text = text;
}
public void CopyDirectory(String source, String destination)
{
if (!Directory.Exists(source))
{
return;
}
if (!Directory.Exists(destination))
{
Directory.CreateDirectory(destination);
}
DirectoryInfo info = new DirectoryInfo(source);
foreach (FileSystemInfo fsi in info.GetFileSystemInfos())
{
String destName = Path.Combine(destination, fsi.Name);
if (fsi is System.IO.FileInfo)
{
File.Copy(fsi.FullName, destName, true);
}
else
{
CopyDirectory(fsi.FullName, destName);
}
}
}
public String GetShortPathName(String path)
{
StringBuilder sbPath = new StringBuilder(255);
GetShortPathName(path, sbPath, sbPath.Capacity);
return sbPath.ToString();
}
#region = Extern =
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern int GetShortPathName(
[MarshalAs(UnmanagedType.LPTStr)]
String path,
[MarshalAs(UnmanagedType.LPTStr)]
StringBuilder shortPath,
int shortPathLength
);
#endregion
private void btnAbout_Click(object sender, EventArgs e)
{
String msg = "VisualStudio cs文件头备注批量修改工具" Environment.NewLine
"作者:StarPeng" Environment.NewLine
"邮箱:StarPeng@vip.qq.com" Environment.NewLine
"说明:批量修改的是VS模板缓存中的文件,如果VS重建模板缓存文件后,所有批注头将会消失!备份文件放置到D:\\CommentHelper目录下。" Environment.NewLine
;
MessageBox.Show(msg, "关于");
}
}
}