Click here to Skip to main content
15,885,782 members
Articles / Programming Languages / C#
Tip/Trick

VS 2017 Offline Installation Folder Cleanup

Rate me:
Please Sign up or sign in to vote.
4.97/5 (10 votes)
21 Aug 2017CPOL1 min read 16.7K   587   8   5
Cleanup the VS 2017 offline installation folder of old versions

Introduction

Sometimes, you have to wonder what the folks at Microsoft are thinking. If you have tried the offline installation mode with the new Visual Studio 2017 installation mechanism, you will see that on subsequent update downloads, your offline folder size will increase, that is to say the old versions are not deleted and the full install 28Gb directory will just get bigger.

To help relieve you of the burden on disk, I have written a small program that will cleanup that folder (it should be in the VS installer, but it's not).

How To Use It

The usage is pretty simple, just copy the EXE and the DLL file into the offline installation folder and run from there, and you will see the output like below:

Image 1

What's in the Code

The code is very simple, it reads the catalog.json file and goes through all the folders of VS2017 and tries to match the package id and version number, if none is found, it presumes it is an old version and moves that folder to the - old - folder, where you can delete it later.

C#
class Program
{
    static void Main(string[] args)
    {
        string path = Directory.GetCurrentDirectory();
        if (path.EndsWith("\\") == false) path += "\\";
        string catalog = path + "catalog.json";
        if (File.Exists(catalog) == false)
        {
            Console.WriteLine("Please copy this program to the VS2017 offline folder.");
            return;
        }
        string old = Directory.CreateDirectory(path + "- old -").FullName;
        var o = fastJSON.JSON.ToDynamic(File.ReadAllText(catalog));

        var folders = Directory.GetDirectories(path);
        var packages = o["packages"];
        foreach (var folder in folders)
        {
            var foldername = Path.GetFileName(folder);
            var splitstring = foldername.Split(',');
            bool found = false;
            if (splitstring.Length > 1)
            {
                foreach (var package in packages)
                {
                    if (package["id"].ToString() == splitstring[0] && 
                        package["version"] == splitstring[1].Split('=')[1].Trim())
                    {
                        found = true;
                        break;
                    }
                }
            }
            else
            {
                found = true;
                Console.WriteLine("not found in packages : " + foldername);
            }

            if (found == false)
            {
                // move directory
                Console.WriteLine("moving folder : " + foldername);
                Directory.Move(folder, old + "\\" + foldername);
            }
        }
        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();
    }
}

Points of Interest

I'm using my fastJSON library to parse the catalog.json file in the installation folder and using ToDynamic() to simplify the coding.

History

  • 21st August 2017: Initial version 1.0.0

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect -
United Kingdom United Kingdom
Mehdi first started programming when he was 8 on BBC+128k machine in 6512 processor language, after various hardware and software changes he eventually came across .net and c# which he has been using since v1.0.
He is formally educated as a system analyst Industrial engineer, but his programming passion continues.

* Mehdi is the 5th person to get 6 out of 7 Platinum's on Code-Project (13th Jan'12)
* Mehdi is the 3rd person to get 7 out of 7 Platinum's on Code-Project (26th Aug'16)

Comments and Discussions

 
Praisegreat job Pin
surbahar5317-Aug-20 0:03
surbahar5317-Aug-20 0:03 
PraiseGreat idea but possibly superseded by the recent 15.3 release Pin
Ian Yates22-Aug-17 3:00
Ian Yates22-Aug-17 3:00 
Visit Visual Studio 2017 15.3 Release Notes[^] and look for --clean.


I've used the new --verify and --fix options to verify my offline sources - this is great because I did have a corrupt package way back in the 2017 RC days and the error handling around that was horrible.

The --clean option is not well documented but is supposed to do, in a supported manner, what your code does.

I personally have been storing my offline installation source on a NAS running Windows Server 2012 with data deduplication enabled on the drive. It's *amazing* how much space is saved since most of the version-to-version package contents really doesn't change all that much Smile | :)

I really like that you've done this even if it's soon not to be needed.
GeneralRe: Great idea but possibly superseded by the recent 15.3 release Pin
Mehdi Gholam22-Aug-17 6:01
Mehdi Gholam22-Aug-17 6:01 
PraiseMy vote of 5 Pin
dandy7221-Aug-17 4:18
dandy7221-Aug-17 4:18 

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.