using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Text.RegularExpressions; namespace SetAssemblyVersion { class Program { static List assemblyInfoFiles = new List(); static string newVersion; static void Main(string[] args) { Console.WriteLine("Searching...\n"); string path = Environment.CurrentDirectory; ScanFolder(path); if (assemblyInfoFiles.Count == 0) { Console.WriteLine("I didn't find any AssemblyInfo.cs files below the current directory.\nPress Enter to quit"); Console.ReadLine(); } else { Regex regex = new Regex(@"\d+\.\d+\.\d+\.\d+"); Console.WriteLine("Found {0} AssemblyInfo.cs file{1}. Please specify the new version number:", assemblyInfoFiles.Count, assemblyInfoFiles.Count == 1 ? "" : "s"); while(true) { newVersion = Console.ReadLine(); Match check = regex.Match(newVersion); if (!check.Success || (check.Value != newVersion)) { Console.WriteLine("New value is not correct format (must be of the form 1.2.3.4):"); } else break; } ProcessAssemblyInfoFiles(); Console.WriteLine("Done"); } } static void ScanFolder(string folder) { foreach (string subFolder in Directory.GetDirectories(folder)) { string folderName = subFolder.Substring(subFolder.LastIndexOf("\\") + 1); ScanFolder(subFolder); if (folderName == "Properties") { ScanForAssemblyInfo(subFolder); } } } static void ScanForAssemblyInfo(string folder) { string assemblyInfo = Path.Combine(folder, "AssemblyInfo.cs"); if (File.Exists(assemblyInfo)) { assemblyInfoFiles.Add(assemblyInfo); } } static void ProcessAssemblyInfoFiles() { Regex regex = new Regex(@"(\[assembly\: Assembly.*?Version\(\"")(.*?)(\""\)\])", RegexOptions.IgnoreCase); foreach (var file in assemblyInfoFiles) { string fileContent = File.ReadAllText(file); fileContent = regex.Replace(fileContent, ReplaceVersion); Console.WriteLine("Writing {0}...", file); File.WriteAllText(file, fileContent); } } static string ReplaceVersion(Match m) { return m.Groups[1] + newVersion + m.Groups[3]; } } }