清理Visual Studio 2017旧版本离线安装文件

原文地址:清理Visual Studio 2017旧版本离线安装文件

说明

Visual Studio 2017内容庞大,如果在线安装相当耗时,因此多数情况下采用离线安装方式。比如用下面的命令:

vs_enterprise.exe --layout F:\Dev\VisualStudio\VS2017Ent --lang zh-CN

上面的命令将安装包下载到F:\Dev\VisualStudio\VS2017Ent目录下,用户可以使用本地文件离线安装 Visual Studio 2017。但是,当再次执行上面命令更新离线安装包后,旧版本的安装包不会自动删除,导致离线安装包越来越庞大,存在大量无用安装文件。

  • 下面的代码用来清理旧版本离线安装文件。
  • C#语言。
  • 程序为控制台应用程序。
  • Visual Studio 2017 测试通过。

具体代码

文件名:DelLegacyFolder.cs

using System;
using System.Collections.Generic;
using System.IO;

namespace SampleCode
{
    class DelLegacyFolder
    {
        static void Main(string[] args)
        {
            try
            {
                //string dirPath = @"F:\Dev\VisualStudio\VS2017Ent";
                string dirPath = @".";

                List<string> dirs = new List<string>(Directory.EnumerateDirectories(dirPath));

                List<string> olderFolder = new List<string>();

                //不自行排序,使用系统自己的排序方式
                //dirs.Sort();

                int count = 0;
                for (int i = 0; i < dirs.Count - 1; i++)
                {
                    string[] OlderFolderName = dirs[i].Split(',');
                    string[] NewerFolderName = dirs[i + 1].Split(',');

                    if ((OlderFolderName[0] == NewerFolderName[0]) 
                        && (OlderFolderName[1] != NewerFolderName[1]) 
                        && (OlderFolderName.Length == NewerFolderName.Length))
                    {
                        bool ifNeedOperation = false;
                        switch (OlderFolderName.Length)
                        {
                            case 2:
                                ifNeedOperation = true;
                                break;
                            case 3:
                                if ((OlderFolderName[2] == NewerFolderName[2]))
                                    ifNeedOperation = true;
                                break;
                            case 4:
                                if ((OlderFolderName[2] == NewerFolderName[2]) 
                                    && (OlderFolderName[3] == NewerFolderName[3]))
                                    ifNeedOperation = true;
                                break;
                            default:
                                break;
                        }

                        if (ifNeedOperation)
                        {
                            string[] olderVersion = OlderFolderName[1].Substring(OlderFolderName[1].IndexOf("=") + 1).Split('.');
                            string[] newerVersion = NewerFolderName[1].Substring(NewerFolderName[1].IndexOf("=") + 1).Split('.');
                            for (int x = 0; x < olderVersion.Length; x++)
                            {
                                if (Int32.Parse(olderVersion[x]) < Int32.Parse(newerVersion[x]))
                                {
                                    Console.ForegroundColor = ConsoleColor.Green;
                                    Console.WriteLine(dirs[i]);
                                    Console.ForegroundColor = ConsoleColor.White;
                                    Console.WriteLine(dirs[i + 1]);
                                    olderFolder.Add(dirs[i]);
                                    count += 1;
                                    i += 1;
                                    break;
                                }
                                else if (Int32.Parse(olderVersion[x]) > Int32.Parse(newerVersion[x]))
                                {
                                    Console.ForegroundColor = ConsoleColor.White;
                                    Console.WriteLine(dirs[i]);
                                    Console.ForegroundColor = ConsoleColor.Green;
                                    Console.WriteLine(dirs[i + 1]);
                                    olderFolder.Add(dirs[i + 1]);
                                    count += 1;
                                    i += 1;
                                    break;
                                }
                            }
                        }
                    }
                }

                Console.ForegroundColor = ConsoleColor.White;
                Console.WriteLine("总共有 {0} 个旧文件夹,是否删除? y--删除 n--不删除", count);
                string yesDel = Console.ReadLine().Trim().ToUpper();
                if (yesDel == "Y")
                {
                    for (int i = 0; i < olderFolder.Count; i++)
                    {
                        Directory.Delete(olderFolder[i], true);
                    }
                }
            }
            catch (UnauthorizedAccessException UAEx)
            {
                Console.WriteLine(UAEx.Message);
            }
            catch (PathTooLongException PathEx)
            {
                Console.WriteLine(PathEx.Message);
            }

            Console.WriteLine("执行完毕,按任意键退出。");
            Console.ReadKey();
        }
    }
}

生成的可以执行文件放置在离线安装包文件目录(例如上面命令行所示的F:\Dev\VisualStudio\VS2017Ent)下面,执行程序可看到如下效果:

在这里插入图片描述

编译好的可执行文件: 点击下载

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值