/SourceFolderCleanup

WinForms utility for freeing up disk space under source folder (bin, obj folders)

Primary LanguageC#

This is a small utility I'm working on to free up disk space on my Surface machine which has a relatively small 256GB drive. My main repo folder has large package/, bin/ and obj/ folders that accumulate a lot of stuff that doesn't need to be there all the time. There might something out there that already does this nicely, but as usual, this is just something I like working on.

img

This makes use of my WinForms.Library project to handle the form data binding with a ControlBinder<T> object, incidentally. I'm also using my JsonSettings.Library project to handle settings persistence in json. My Settings model is here.

private void frmMain_Load(object sender, System.EventArgs e)
{
	var monthValues = new int[] { 3, 6, 9, 12 }.Select(i => new ListItem<int>(i, i.ToString()));

	_settings = SettingsBase.Load<Settings>();
	_binder = new ControlBinder<Settings>();            

	_binder.Add(tbSourcePath, model => model.SourcePath);
	_binder.Add(chkDelete, model => model.Delete);
	_binder.Add(chkDeleteBinObj, model => model.DeleteBinAndObj);
	_binder.Add(chkDeletePackages, model => model.DeletePackages);
	
	_binder.AddItems(cbDeleteMonths,
		(model) => model.DeleteMonthsOld = cbDeleteMonths.GetValue<int>(),
		(model) => cbDeleteMonths.SetValue(model.DeleteMonthsOld), monthValues);

	_binder.Add(chkArchive, model => model.Archive);
	_binder.Add(tbArchivePath, model => model.ArchivePath);
	_binder.AddItems(cbArchiveMonths,
		(model) => model.ArchiveMonthsOld = cbArchiveMonths.GetValue<int>(),
		(model) => cbArchiveMonths.SetValue(model.ArchiveMonthsOld), monthValues);

	_binder.Document = _settings;
	chkArchive_CheckedChanged(null, new EventArgs());
	chkDelete_CheckedChanged(null, new EventArgs());
}

I'm also using WinForm.Library's EnumFiles method to find bin and obj directories. The base implementation looks like this:

public IEnumerable<string> GetSubfoldersNamed(string parentPath, string[] includeNames, string[] excludeNames = null)
{
	List<string> results = new List<string>();

	FileSystem.EnumFiles(parentPath, "*", directoryFound: (di) =>
	{
		if (includeNames.Any(name => di.Name.Equals(name)) && (excludeNames?.All(name => !di.FullName.Contains(name)) ?? true))
		{
			results.Add(di.FullName);
			return EnumFileResult.NextFolder;
		}

		return EnumFileResult.Continue;
	});

	return results;
}