Click here to Skip to main content
13,200,729 members (53,402 online)
Click here to Skip to main content
Add your own
alternative version

Stats

6K views
54 downloads
4 bookmarked
Posted 9 Oct 2017

How to Really Clean the Solution

, 11 Oct 2017
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Laszlo Voros
Software Developer (Senior)
Germany Germany
No Biography provided

You may also be interested in...

Pro
Pro

Comments and Discussions

 
GeneralMy vote of 3 Pin
Jim_Snyder11-Oct-17 3:51
memberJim_Snyder11-Oct-17 3:51 
QuestionNuke it from orbit, it's the only way to be sure... Pin
dandy7210-Oct-17 5:42
memberdandy7210-Oct-17 5:42 
AnswerRe: Nuke it from orbit, it's the only way to be sure... Pin
wkempf10-Oct-17 7:59
memberwkempf10-Oct-17 7:59 
QuestionBatch file Pin
tbayart10-Oct-17 3:52
membertbayart10-Oct-17 3:52 
AnswerRe: Batch file Pin
Laszlo Voros11-Oct-17 20:44
memberLaszlo Voros11-Oct-17 20:44 
QuestionPowerShell One Liner Pin
wkempf9-Oct-17 4:01
memberwkempf9-Oct-17 4:01 
AnswerRe: PowerShell One Liner Pin
Laszlo Voros9-Oct-17 4:23
memberLaszlo Voros9-Oct-17 4:23 
GeneralRe: PowerShell One Liner Pin
wkempf9-Oct-17 7:24
memberwkempf9-Oct-17 7:24 
GeneralRe: PowerShell One Liner Pin
PeejayAdams10-Oct-17 2:47
memberPeejayAdams10-Oct-17 2:47 
GeneralRe: PowerShell One Liner Pin
wkempf10-Oct-17 7:12
memberwkempf10-Oct-17 7:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171020.1 | Last Updated 12 Oct 2017
Article Copyright 2017 by Laszlo Voros
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid