Inserting copyright notice/banner in all source code files in Visual Studio 2012

Solution 1:

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).

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.

Method 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>

Method 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);
}

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.

Method 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\

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$
    {
    }
}

Solution 2:

I'm going to add here a PowerShell script I found in this post: Powershell – Copyright header generator script. It captures what I had in mind before posting the question...

param($target = "C:\MyProject", $companyname = "My Company")

$header = "//-----------------------------------------------------------------------

// <copyright file=""{0}"" company=""{1}"">

// Copyright (c) {1}. All rights reserved.

// </copyright>

//-----------------------------------------------------------------------`r`n"

function Write-Header ($file)
{
    $content = Get-Content $file

    $filename = Split-Path -Leaf $file

    $fileheader = $header -f $filename,$companyname

    Set-Content $file $fileheader

    Add-Content $file $content
}

Get-ChildItem $target -Recurse | ? { $_.Extension -like ".cs" } | % `
{
    Write-Header $_.PSPath.Split(":", 3)[2]
}

I wrote about it with minor modifications to adapt it to my needs:

Inserting copyright notice/banner/header in all source code files with PowerShell