How to update .NET Framework version in all projects at once

Sometimes while working with massive solutions, we need to update .NET framework versions in all of the projects at once. Doing so manually is way too cumbersome and time-consuming. One can easily replace TargetFrameworkVersion string within project files in a loop.

<div>string folderPath = @"C:\MySolution";</div>
<div>string <code class="css string">newVersion = <span class="pl-s"><span class="pl-pds">"</span>v4.6.1<span class="pl-pds">"</span></span>;</code></div>
<div class="line number53 index52 alt2"><code class="css plain">int count = </code><code class="css value">0</code><code class="css plain">;</code></div>
<div class="line number54 index53 alt1"><code class="css spaces">            </code><code class="css plain">try</code></div>
<div class="line number55 index54 alt2"><code class="css spaces">            </code><code class="css plain">{</code></div>
<div class="line number56 index55 alt1"><code class="css spaces">                </code><code class="css plain">string[] files = Directory.GetFiles(folderPath, </code><code class="css string">"*.csproj"</code><code class="css plain">, SearchOption.AllDirectories);</code></div>
<div class="line number57 index56 alt2"><code class="css spaces">                </code><code class="css plain">foreach (var file in files)</code></div>
<div class="line number58 index57 alt1"><code class="css spaces">                </code><code class="css plain">{</code></div>
<div class="line number59 index58 alt2"></div>
<div class="line number60 index59 alt1"><code class="css spaces">                    </code><code class="css plain">string text = File.ReadAllText(file);</code></div>
<div class="line number61 index60 alt2"><code class="css spaces">                    string pattern = @"v([\d\.]+)"; text = Regex.Replace(text, pattern, $"v{newVersion}"); </code></div>
<div class="line number64 index63 alt1"><code class="css spaces">                    </code><code class="css plain">File.WriteAllText(file, text);</code></div>
<div class="line number65 index64 alt2"><code class="css spaces">                    </code><code class="css plain">Console.WriteLine($</code><code class="css string">"{++count}. Updated {file.Split('\\').LastOrDefault()}"</code><code class="css plain">);</code></div>
<div class="line number66 index65 alt1"><code class="css spaces">                </code><code class="css plain">}</code></div>
<div class="line number67 index66 alt2"><code class="css spaces">            </code><code class="css plain">}</code></div>
<div class="line number68 index67 alt1"><code class="css spaces">            </code><code class="css plain">catch (Exception exception)</code></div>
<div class="line number69 index68 alt2"><code class="css spaces">            </code><code class="css plain">{</code></div>
<div class="line number70 index69 alt1"><code class="css spaces">                </code><code class="css plain">Console.WriteLine($</code><code class="css string">"Error: {exception.Message}"</code><code class="css plain">);</code></div>
<div class="line number71 index70 alt2"><code class="css spaces">            </code><code class="css plain">}</code></div>
<div class="line number72 index71 alt1"></div>
<div class="line number73 index72 alt2"><code class="css spaces">            </code><code class="css plain">Console.WriteLine();</code></div>
<div class="line number74 index73 alt1"><code class="css spaces">            </code><code class="css plain">Console.WriteLine($</code><code class="css string">"Updated Framework version in {count} projects."</code><code class="css plain">);</code></div>
<div class="line number75 index74 alt2">

Grab the full ready-to-go console program from this Gist or code from below.

To avoid some copying and pasting change defaultFolderPath to folder where your solution lives and defaultFrameworkVersion to the version of your choosing and up you go…


using System;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;

namespace UpgradeFrameworkInProjects
{
	class Program
	{
		private const string exitCode = "0";
		private const string defaultFolderPath = @"C:\Solutions\MySolution";
		private const string defaultFrameworkVersion = "v4.6.1";

		static void Main(string[] args)
		{
			while (UpgradeFrameworkInProjects())
			{
				Console.WriteLine();
				Console.WriteLine();

				Console.WriteLine("Press any key to restart.");
				Console.WriteLine($"Type {exitCode} to exit the program.");
				Console.ReadKey();
			}
		}

		private static bool UpgradeFrameworkInProjects()
		{
			Console.WriteLine("---");
			Console.WriteLine("This program updates .NET Framework version in all projects in a solution.\n");

			Console.WriteLine($"Hit enter to run in this solution: {defaultFolderPath}");
			Console.Write("Or enter path to solution folder: ");
			string folderPath = Console.ReadLine().Trim();

			if (string.IsNullOrWhiteSpace(folderPath))
				folderPath = defaultFolderPath;
			else if (folderPath == exitCode)
				return false;

			Console.WriteLine($"Hit enter to update projects to version: {defaultFrameworkVersion}");
			Console.Write("Or enter the version: v");
			string newVersion = Console.ReadLine().Trim();

			if (string.IsNullOrWhiteSpace(newVersion))
				newVersion = defaultFrameworkVersion;
			else if (newVersion == exitCode)
				return false;

			if (newVersion.ToLower().StartsWith("v"))
				newVersion = newVersion.Remove(0, 1);

			int count = 0;
			try
			{
				string[] files = Directory.GetFiles(folderPath, "*.csproj", SearchOption.AllDirectories);
				foreach (var file in files)
				{

					string text = File.ReadAllText(file);
					string pattern = @"v([\d\.]+)";
					text = Regex.Replace(text, pattern, $"v{newVersion}");

					File.WriteAllText(file, text);
					Console.WriteLine($"{++count}. Updated {file.Split('\\').LastOrDefault()}");
				}
			}
			catch (Exception exception)
			{
				Console.WriteLine($"Error: {exception.Message}");
			}

			Console.WriteLine();
			Console.WriteLine($"Updated Framework version in {count} projects.");

			return true;
		}

Advertisements
Create your website at WordPress.com
Get started
%d bloggers like this:
search previous next tag category expand menu location phone mail time cart zoom edit close