A tool add your cs file head comment
2011-12-19 09:45 barbarossia 阅读(380) 评论(0) 编辑 收藏 举报When we want to add some head comment, we will copy, paste sth on file head. But we can also through this toolkit to add sth on file.
The code is:
class Program
{
private const string license = "//*********************************************************\r\n//\r\n// Copyright (c) Microsoft 2011. All rights reserved.\r\n// This code is licensed under your Microsoft OEM Services support\r\n// services description or work order.\r\n// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF\r\n// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY\r\n// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR\r\n// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.\r\n//\r\n//*********************************************************\r\n\r\n";
private const string directory = @"D:\OASICH\OASICH\Branch\IterationFour\src\Data\DataAccess";
static void Main(string[] args)
{
string[] files = Directory.GetFiles(directory, "*.cs", SearchOption.AllDirectories);
foreach (string file in files)
{
FileInfo fileInfo = new FileInfo(file);
fileInfo.Attributes &= (int.MaxValue - FileAttributes.ReadOnly);
using (FileStream stream = new FileStream(file, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
{
StreamReader reader = new StreamReader(stream, true);
string text = reader.ReadToEnd();
if (!text.StartsWith(license))
text = license + text;
stream.Seek(0, SeekOrigin.Begin);
StreamWriter writer = new StreamWriter(stream, Encoding.UTF8);
writer.Write(text);
writer.Flush();
}
}
}
}
The sample code is here.