在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#语言基础 → VS批注批量修改CommentHelper

VS批注批量修改CommentHelper

C#语言基础

下载此实例
  • 开发语言:C#
  • 实例大小:0.27M
  • 下载次数:9
  • 浏览次数:315
  • 发布时间:2015-09-23
  • 实例类别:C#语言基础
  • 发 布 人:crazycode
  • 文件格式:.rar
  • 所需积分: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, "关于");
        }

    }
}

标签:

实例下载地址

VS批注批量修改CommentHelper

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警