Deleting folders is a risky business Deleting folders 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!)

/// &lt;summary&gt;
/// carefully remove directory and its files
/// verify the directory is located under the %temp%
/// and it is not e.g. C:\
/// &lt;/summary&gt;
/// &lt;param name="dir"&gt;&lt;/param&gt;        
public static void DeleteTempDirShallow(string dir)
{
    // check if it is an invalid directory path,
    // e.g. a disk drive or just a bad string
    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(); // just clean up the empty dir
    }
}


Delete temporary directory created by current user (recursive!)

public static void DeleteTempDirRecursive(string dir)
{
    // check if it is an invalid directory path,
    // e.g. a disk drive or just a bad string
    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&lt;string&gt; systemDirs = new List&lt;string&gt;
{
    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),
};

/// &lt;summary&gt;
/// check if the argument dir is adisk drive,
/// is a system (sub-)folder
/// and should not be erased
/// &lt;/summary&gt;
/// &lt;param name="dir"&gt;&lt;/param&gt;
/// &lt;returns&gt;&lt;/returns&gt;
public static bool IsSystemOrRootDir(string dir)
{
    // check if it is an invalid directory path,
    // or just a bad string
    if (! Directory.Exists(dir)) return true;

    DirectoryInfo dirInfo = new DirectoryInfo(dir);

    // is it a root (disk drive)? 
    if (dirInfo.Parent == null) return true;

    bool result = false;
    systemDirs.ForEach(sysDir =&gt; { result = result ||
                                   dirInfo.FullName.Contains(sysDir); } );
    return result;
}


References

How to check if directory 1 is a subdirectory of dir2 and vice versa
How to delete all files and folders in a directory?
When all you have is a hammer, every problem looks like a nail.

You're trying to do something with code, that's not the code's job. Delete all this code and tackle the problem at the root: make sure your code runs with the appropriate permissions.
It's that simple.
-- My humble opinion



Deleting folders is a risky business

Indeed! If your running code doesn't have permission to wipe out your hard drive, you have some exceptions to deal with, i.e. you should wrap I/O calls in a try...catch block.