每次编译代码成功后都会生成obj/bin等文件夹。因此想清理这些文件夹,无奈工程数量较多,每个都要手动去删除比较累。就想到用代码写个小工具删除,当然也可以利用bat批处理删除。
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace ClearBinObj
{
internal class Program
{
private static void Main(string[] args)
{
try
{
var rootFolder = Directory.GetCurrentDirectory();
Console.WriteLine("-----------开始清理obj文件夹和bin文件夹------------");
DeleteObjBin(rootFolder);
Console.WriteLine("-------------------已完成清理--------------------");
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.Read();
}
}
private static void DeleteObjBin(string rootPath)
{
try
{
string binFolderPath = Path.Combine(rootPath, "bin");
string objFolderPath = Path.Combine(rootPath, "obj");
if (Directory.Exists(binFolderPath))
{
Directory.Delete(binFolderPath, true);
Console.WriteLine($"删除文件夹:{binFolderPath}");
}
if (Directory.Exists(objFolderPath))
{
Directory.Delete(objFolderPath, true);
Console.WriteLine($"删除文件夹:{objFolderPath}");
}
var subFolders = Directory.GetDirectories(rootPath);
foreach (var folder in subFolders)
{
DeleteObjBin(folder);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}