Category Archives: csproj

Clean Visual Studio Solution

Today, every project we work on big or small, easy or complex, small team or large team  is probably on Source Control. The source control of course can be git, VSTS, SVN etc.

Still, there are times where you need to share your code as zip in an email, or shared link. It could be because your customer, colleague or partner do not have access to your source control or simply you have not added your code to Source Control itself.

Now, if you just zip the solution folder and email or share the link then you would include folders like bin, obj, packages or files like .sou, .user etc. These files are not required to build the solution. These files increase your zip file size significantly. The solution is simple, delete all the files which are not required. However, what if you have over 50 projects in the solution? And what if you have to this activity multiple times? It is too much of manual effort to do perform this activity.

I had a similar issue in my one of  my engagements recently.  However, instead of spending hours to do this manual work, I decided to automate the process by creating a small console app. The app deletes all the unwanted folders and files recursively from the solution. I have included following folders and files as per my requirements as the part of  deletion list:

Folders:  bin, obj, TestResults, packages
Files:  "*.vssscc", "*.ncrunchproject", "*.user", "*.suo"

Source code has been shared in GitHub here.

Alternatively, You can also download the executable directly from here.

Hope it helps some you to save your time and be more productive. Please do provide your comments and feedback.

Advertisements

Edit csproj Project file programatically

In my current engagement, we have more than 80 projects in a solution (don’t ask me why :)). Recently, as per quality guidelines, we needed to make few changes to each project.
For example:  Treat warnings as errors, enable code analysis for each project, sign assembly etc.

I realized doing it manually can take me entire day so I spent few mins to create a small script in C# to save my time. Here is the code snippet:

using System.Collections.Generic;
using System.Linq;
using Microsoft.Build.Evaluation;
class Program
{
   static void Main(string[] args)
   {
     var projectList = new List()
     {
        // Your Project file paths
     };
     foreach (var project in projectList)
     {
        var projectCollection = new ProjectCollection();
        var proj = projectCollection.LoadProject(project);
        // Select Debug configuration
         var debugPropertyGroup = 
           proj.Xml.PropertyGroups.First(
           e => e.Condition == " '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ");
         debugPropertyGroup.SetProperty("TreatWarningsAsErrors", "true");
         debugPropertyGroup.SetProperty("RunCodeAnalysis", "true");
      
        // Select Release configuration
        var releasePropertyGroup = 
           proj.Xml.PropertyGroups.First(
           e => e.Condition == " '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ");
        releasePropertyGroup.SetProperty("TreatWarningsAsErrors", "true");
        releasePropertyGroup.SetProperty("RunCodeAnalysis", "true");
     
        //Sign assembly with a with strong name key
        proj.SetProperty("SignAssembly", "true");
        proj.SetProperty("AssemblyOriginatorKeyFile", "test.pfx");

        //Save
        proj.Save();
      }
    } 
}

Hope it helps save some time for some of you 🙂

Update: I have created a command line utility for the solution and added it to GitHub. This utility accepts different set arguments to based on operations required to be performed. Please refer to ReadMe.md in GitHub for more details.