Написах проста помощна програма, която преминава през всички C# файлове в моя проект и актуализира текста за авторски права в горната част.
Например файл може да изглежда така;
//Copyright My Company, © 2009-2010
Програмата трябва да актуализира текста, за да изглежда така;
//Copyright My Company, © 2009-2010
Въпреки това кодът, който съм написал, води до това;
//Copyright My Company, � 2009-2011
Ето кода, който използвам;
public bool ModifyFile(string filePath, List<string> targetText, string replacementText)
{
if (!File.Exists(filePath)) return false;
if (targetText == null || targetText.Count == 0) return false;
if (string.IsNullOrEmpty(replacementText)) return false;
string modifiedFileContent = string.Empty;
bool hasContentChanged = false;
//Read in the file content
using (StreamReader reader = File.OpenText(filePath))
{
string file = reader.ReadToEnd();
//Replace any target text with the replacement text
foreach (string text in targetText)
modifiedFileContent = file.Replace(text, replacementText);
if (!file.Equals(modifiedFileContent))
hasContentChanged = true;
}
//If we haven't modified the file, dont bother saving it
if (!hasContentChanged) return false;
//Write the modifications back to the file
using (StreamWriter writer = new StreamWriter(filePath))
{
writer.Write(modifiedFileContent);
}
return true;
}
Всяка помощ/предложения се оценяват. Благодаря!