How to: Add a License to Code Files

  • 3 minutes to read

The Add Copyright Header code provider and the h template allow you to add a copyright comment at the beginning of a source code file.

This comment can contain a file name, company name, author, and a license.

HowTo_Header_GIF

The following example shows how to customize the h template to add a license to a copyright comment.

  1. Choose the CodeRush | Code Templates... item in the CodeRush menu to access the code templates options page.

    HowTo_Header_2

  2. Select the 1-key | h template.

    HowTo_Header1

CodeRush stores the default copyright header as an h template expansion in the template editor. The default copyright header uses the following String Providers:

  • «?FileName» — returns the current file's full path.
  • «?GetUserFirstName» — returns the "First Name" value from the IDE | User Info options page.
  • «?GetUserLastName» — returns the "Last Name" value from the IDE | User Info options page.
  • «?GetUserInfo(key)» — returns an arbitrary value from the IDE | User Info options page. You can specify which value to return using the key parameter.
  1. Add a license to the default copyright header. For example, the MIT license:

    HowTo_Header_2

  2. Click OK to save the changes and close the Templates options page.

    HowTo_Header_2

  3. Open the IDE | User Info options page and configure the "Company", "First Name", and "Last Name" fields for the default copyright header:

    UserInfo

  4. Open a source file and perform one of the following actions:

Add a License to All Project Files

  1. Open the Editor | C# (Visual Basic) | Code Cleanup options page and choose the Add copyright header rule.

    AddHeader

  2. Configure the Add copyright header rule:

  • Enable "Apply In Action" to allow CodeRush to apply the Add copyright header rule when you call code cleanup.

  • Enable "Apply on Save" and Apply Code Cleanup when saving a document options to allow CodeRush to apply this rule in code cleanup before a file is saved.

    AddHeader

  • Click OK to save and apply the settings.

  1. Clean up your project:
  • Right-click the project you want to clean in the Solution Explorer.
  • Select CodeRush Cleanup in the context menu.

    CleanUpProject

or

  • Save your project file if you allow CodeRush to apply this rule in code cleanup on save.
NOTE

If you do not want CodeRush to modify the existing copyright header after code cleanup, enable the Do not replace the existing copyright header during code cleanup option on the Editor | C# (Visual Basic) | Programming Style options page.