private static readonly string _filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "CodeFolder");
private static Dictionary<string, string> _codes = new Dictionary<string, string>();
//call repository codes changed
private static readonly Regex _oldTextRegex = new Regex(@"XXX\.XX\(repositoryIP, repositoryPort, XXX\.\w+\)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
private static readonly Regex _saveText = new Regex(@"XXX\.\w+", RegexOptions.Compiled);
private const string NEW_TEXT = "MethodName({0}, dataFilePathDic[{0}])";
static void Main(string[] args)
{
LoadFile();
ReplaceCodes();
Console.WriteLine("Please type anykey to exist");
Console.ReadKey();
}
private static void ReplaceCodes()
{
foreach (var codes in _codes)
{
string newCodes = _oldTextRegex.Replace(codes.Value, ReplaceDetail);
WriteFile(codes.Key, newCodes);
Console.WriteLine("Replace " + codes.Key + " code completed");
}
}
private static string ReplaceDetail(Match match)
{
var saveCodeMatch = _saveText.Match(match.Value);
if (saveCodeMatch.Success)
{
return string.Format(NEW_TEXT, saveCodeMatch.Value);
}
return "[not found]";
}
private static void LoadFile()
{
if (!Directory.Exists(_filePath))
{
Directory.CreateDirectory(_filePath);
return;
}
DirectoryInfo fileDirectory = new DirectoryInfo(_filePath);
var files = fileDirectory.GetFiles("*.cs");
foreach (var file in files)
{
var fs = file.OpenRead();
StreamReader sr = new StreamReader(fs);
_codes[file.FullName] = sr.ReadToEnd();
sr.Close();
sr.Dispose();
fs.Close();
fs.Dispose();
}
}
public static void WriteFile(string filePath, string content)
{
using (FileStream stream = new FileStream(filePath, FileMode.Create))
{
StreamWriter writer = new StreamWriter(stream);
writer.Write(content);
writer.Close();
}
}