且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Visual Studio 2012中的所有源代码文件中插入版权声明/横幅

更新时间:2023-11-08 16:17:28

您可以创建一个新代码段,然后键入cp + double tab将通知插入您想要的位置(不必说您可以将关键字更改为您想要的任何内容).

You could create a new snippet and just type cp + double tab to insert the notice where you want (needless to say you can change the keyword to whatever you want).

它的唯一问题是,据我所知,摘要不支持时间功能,因此使用此技术似乎无法获取日期行的当前时间.一个不太好的解决方法是使时间字段可编辑(类似于mbox代码段的工作方式),然后手动插入时间.

The only problem with it is, from what I'm aware, snippets do not support time functions, so getting the current time for your date line seems impossible with this technique. A not so good workaround for this is to make the time fields editable (similar to how the mbox snippet works) and just insert the time manually.

这是一个有关代码段外观的示例.下面的代码段将自动获得类名称,并将版权声明插入您键入版权"并双击的位置.

Here's an example on how a snippet looks. The bellow snippet will get the class name automatically and insert the copyright notice in the place where you type 'copyright' and double tab.

方法1

 <?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>Copyright</Title>
      <Shortcut>Copyright</Shortcut>
      <Description>Code snippet for Copyright notice</Description>
      <Author>author name</Author>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
    </Header>
    <Snippet>
      <Declarations>
        <Literal Editable="false">
          <ID>classname</ID>
          <Function>ClassName()</Function>
        </Literal>
      </Declarations>
      <Code Language="csharp">
        <![CDATA[// <copyright file="$classname$" company="My Company Name">
      // Copyright (c) 2012 All Rights Reserved
      // <author>Leniel Macaferi</author>
      // </copyright>
      ]]>
      </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

方法2

此外,这是您可以制作的一个程序示例.

Also, here's an example of a program you can make to do that for you.

List<string> files = new List<string>()
{
    "c:\\Form1.cs",
    "c:\\Form2.cs",
};

foreach (string file in files)
{
    string tempFile = Path.GetFullPath(file) + ".tmp";

    using (StreamReader reader = new StreamReader(file))
    {
        using (StreamWriter writer = new StreamWriter(tempFile))
        {
            writer.WriteLine(@"// <copyright file=" + Path.GetFileNameWithoutExtension(file) + @" company=My Company Name>
// Copyright (c) 2012 All Rights Reserved
// </copyright>
// <author>Leniel Macaferi</author>
// <date> " + DateTime.Now + @"</date>
// <summary>Class representing a Sample entity</summary>
");

            string line = string.Empty;
            while ((line = reader.ReadLine()) != null)
            {
                writer.WriteLine(line);
            }
        }
    }
    File.Delete(file);
    File.Move(tempFile, file);
}

当然需要某些错误捕获.但这应该为您提供了一个总体思路,即如何围绕该UI构造UI并添加要处理的文件.

Some error catching will be required of course. But this should give you the general idea how to construct an UI around it an add the files you will want to process.

方法3

还可以更改通常在以下位置找到的类的模板:

It's also possible to change the template for your classes that can be usually be found under:

C:\Program Files (x86)\Microsoft Visual Studio <version>\Common7\IDE\ItemTemplates\CSharp\1033\

有时还需要编辑 ItemTemplatesCache 以显示结果.

Sometimes editing ItemTemplatesCache is also necessary to display the results.

以下是基于您的问题的示例模板:

Here's an example template based on your question:

using System;
using System.Collections.Generic;
$if$ ($targetframeworkversion$ >= 3.5)using System.Linq;
$endif$using System.Text;

/* <copyright file=$safeitemrootname$ company="My Company Name">
   Copyright (c) 2012 All Rights Reserved
   </copyright>
   <author>Leniel Macaferi</author>
   <date>$time$</date>
   <summary>Class representing a Sample entity</summary>*/

namespace $rootnamespace$
{
    class $safeitemrootname$
    {
    }
}