Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

How to Really Clean the Solution

0.00/5 (No votes)
11 Oct 2017 3  
How to really clean your Visual Studio Solution

Introduction

I give you a small console-application, which removes all the Debug and Release folders under your Solution.

Background

Normally, you can clean your Solution properly, if you choose Clean Solution in the context-menu or from the Build-menu. But sometimes, not all the files will be deleted. For example, if you change the target framework of a project, the old binaries will not be removed.

In my case, I have had a NETStandard library targeted netsandard1.6. The binaries were built in bin\Debug\netstandard1.6\.

Then I had to change the target framework back to netstandard1.3. The new binaries were built in bin\Debug\netstandard1.3\.

So if I choose "Clean Solution", the netstandard1.3 folder will be cleaned, but netstandard1.6 stays as it was.

The other projects in the solution (e.g. WPF) have a project-reference on this netstandard project, and they still find the netstandard1.6 DLLs and the changes I make after the switch do not have any effect.

But this is only one example, when the solution will not correctly clean. If you have another case, please write it as a comment.

Using the Code

So I made a small console-application, which removes all the Debug* and Release* folders under the solution.

It is very simple:

class Program
{
    static void Main(string[] args)
    {
        var currentDir = Directory.GetCurrentDirectory();
        if (args.Length > 0)
            currentDir = args[0];
        var toDelete = Directory.GetDirectories(currentDir, "Debug*", SearchOption.AllDirectories)
            .Union(Directory.GetDirectories
                  (currentDir, "Release*", SearchOption.AllDirectories)).ToArray();

        foreach (string dir in toDelete)
        {
            try
            {
                Directory.Delete(dir, true);
                Console.WriteLine(dir);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"CANNOT DELETE {dir}");
                Console.WriteLine(ex.ToString());
            }
        }

        Console.WriteLine(new String('+', 80));
        Console.WriteLine($"{toDelete.Length} folders are removed.");
        Console.ReadLine();
    }
}

The solution-folder can be passed through a command line argument. If no argument, the current directory will be taken as root.

As you can see, I collect the folders with Debug* and Release* into a string-array. The SearchOption.AllDirectories do the collecting recursive in the subfolders, too.

Use It Carefully

Use this code carefully! There is no warranty if you delete important files in Debug or Release folders.

Before you start this program, you should close the solution in VS! Otherwise you get an exception, that the folder is not empty.

I hope you do not have any important files under your Debug folder (databases or whatever)!

Use a Sourcecode control!

You can download this program from the attachment of this tip.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here