0 Comments

A C# method that I used to remove common assembly information from a AssemblyInfo.cs.

This method was used with a small FileSystem.cs class.

 

/// <summary>
        /// Remove common assembly info from the given content.
        /// Like:
        /// - AssemblyCompany
        /// - AssemblyCopyright
        /// - AssemblyVersion
        /// - AssemblyFileVersion
        /// </summary>
        public string RemoveCommonAssemblyInfo(string content)
        {
            string result = string.Empty;

            if (!string.IsNullOrWhiteSpace(content))
            {
                result = content;
                var regexes = new List<Regex>();
                regexes.Add(new Regex(@"\r\n\[assembly: AssemblyCompany\(""{1}.*""{1}\)]"));
                regexes.Add(new Regex(@"\r\n\[assembly: AssemblyCopyright\(""{1}.*""{1}\)]"));
                regexes.Add(new Regex(@"\r\n\[assembly: AssemblyVersion\(""{1}.*""{1}\)]"));
                regexes.Add(new Regex(@"\r\n\[assembly: AssemblyFileVersion\(""{1}.*""{1}\)]"));
                regexes.Add(new Regex(@"\r\n\r\n// Version information.*""{1}\)]", RegexOptions.Singleline));

                regexes.ForEach(x =>
                {
                    result = x.Replace(result, string.Empty);
                });
            }

            return result;
        }

namespace Research.Core.Components { using System; using System.Collections.Generic; using System.IO; using System.Threading.Tasks; public class FileSystem { public async Task FindAndReplace(string path, string searchPattern, SearchOption searchOption,

Func<string, string> updateContent) { string[] files = await GetFilesAsync(path, searchPattern, searchOption); foreach (string file in files) { string content = await ReadAllTextAsync(file); content = updateContent(content); await WriteAllTextAsync(file, content); } } public async Task<string[]> GetFilesAsync(string path, string searchPattern, SearchOption searchOption) { return await Task.Factory.StartNew(() => { return Directory.GetFiles(path, searchPattern, searchOption); }); } public async Task<string> ReadAllTextAsync(string path) { string result = await Task.Factory.StartNew(() => { return File.ReadAllText(path); }); return result; } public async Task WriteAllTextAsync(string path, string content) { await Task.Factory.StartNew(() => { File.WriteAllText(path, content); }); } } }

 

To replace all common assembly info found in all AssemblyInfo.cs files in a folder, use the code:

 

var solution = new VsSolution(); var fs = new FileSystem(); Task task = fs.FindAndReplace(@"C:\Projects\Github\roelvanlisdonk\Research\Main",

"AssemblyInfo.cs", System.IO.SearchOption.AllDirectories, solution.RemoveCommonAssemblyInfo); task.Wait();

 

Code can be found at: https://github.com/roelvanlisdonk/Research

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Posts