Introduction
Deleting folder programmatically is extremely dangerous. It is enough for somebody else to change a configuration file or constant variable including the target folder's name to, say C:\, and that's it: the workstation is paralyzed!
The following suggested methods are to prevent such a situation. Their aim is to wrap the low level, possibly recursive file system operation with the necessary validation and clear facade - look twice, delete once.
Delete Temporary Directory Created by Current User (Non recursive!)
public static void DeleteTempDirShallow(string dir)
{
if (! Directory.Exists(dir)) return;
DirectoryInfo userTempDirInfo = new DirectoryInfo(Path.GetTempPath());
DirectoryInfo dirInfo = new DirectoryInfo(dir);
if (dirInfo.FullName.Contains(userTempDirInfo.FullName))
{
foreach (FileInfo file in dirInfo.GetFiles())
file.Delete();
dirInfo.Delete();
}
}
Delete Temporary Directory Created by Current User (Recursive!)
public static void DeleteTempDirRecursive(string dir)
{
if (! Directory.Exists(dir)) return;
DirectoryInfo userTempDirInfo = new DirectoryInfo(Path.GetTempPath());
DirectoryInfo dirInfo = new DirectoryInfo(dir);
if (dirInfo.FullName.Contains(userTempDirInfo.FullName))
{
dirInfo.Delete(recursive: true);
}
}
Do I Try to Delete Any Root or System Folder?
static List<string> systemDirs = new List<string>
{
Environment.GetFolderPath(Environment.SpecialFolder.CommonProgramFiles),
Environment.GetFolderPath(Environment.SpecialFolder.CommonProgramFilesX86),
Environment.GetFolderPath(Environment.SpecialFolder.CommonPrograms),
Environment.GetFolderPath(Environment.SpecialFolder.CommonStartMenu),
Environment.GetFolderPath(Environment.SpecialFolder.CommonStartup),
Environment.GetFolderPath(Environment.SpecialFolder.Fonts),
Environment.GetFolderPath(Environment.SpecialFolder.NetworkShortcuts),
Environment.GetFolderPath(Environment.SpecialFolder.PrinterShortcuts),
Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles),
Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86),
Environment.GetFolderPath(Environment.SpecialFolder.Resources),
Environment.GetFolderPath(Environment.SpecialFolder.StartMenu),
Environment.GetFolderPath(Environment.SpecialFolder.Startup),
Environment.GetFolderPath(Environment.SpecialFolder.System),
Environment.GetFolderPath(Environment.SpecialFolder.SystemX86),
Environment.GetFolderPath(Environment.SpecialFolder.Templates),
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
Environment.GetFolderPath(Environment.SpecialFolder.Windows),
};
public static bool IsSystemOrRootDir(string dir)
{
if (! Directory.Exists(dir)) return true;
DirectoryInfo dirInfo = new DirectoryInfo(dir);
if (dirInfo.Parent == null) return true;
return systemDirs.Any(
sysDir => dirInfo.FullName
.IndexOf(sysDir, StringComparison.OrdinalIgnoreCase) >= 0);
}
References