您可以創建一個新片段,只是類型CP +雙標籤插入您想要的通知(不用說,你可以切換到任何你想要的關鍵字)。
它唯一的問題是,從我所知道的片段不支持時間函數,因此使用這種技術獲取日期行的當前時間似乎是不可能的。一個不太好的解決方法是使時間字段可編輯(類似於mbox代碼段的工作方式),並且只需手動插入時間。
這裏有一個片斷的外觀的例子。下圖片段將自動獲得類名稱,並在您輸入'copyright'和雙標籤的地方插入版權聲明。
方法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
而且,這裏有一個程序,你可以對爲你做的一個例子。
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並添加你想要處理的文件。
方法3
它也可以改變你的類模板通常可下找到:
C:\Program Files (x86)\Microsoft Visual Studio <version>\Common7\IDE\ItemTemplates\CSharp\1033\
有時編輯ItemTemplatesCache也有必要顯示結果。
下面是根據你的問題的示例模板:
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$
{
}
}
您可以使用運行powershell腳本的前/後構建步驟來檢查每個CS文件是否有給定的頭文件,它不? – PhonicUK
@ PhonicUK:這將是一個不錯的選擇。我只是不熟悉PowerShell。 :) –
或者寫一個小的C#應用程序來做到這一點,並運行它? – PhonicUK