Question

I'm working on a Gecko based web browser and I'd like it to delete AppData\Local\Geckofx\ on exit.

I'm currently using this code:

    protected override void OnFormClosing(FormClosingEventArgs e)
    {
        try
        {
            var dir = new DirectoryInfo(@"C:\Users\Admin\AppData\Local\Geckofx\");
            dir.Attributes = dir.Attributes & ~FileAttributes.ReadOnly;
            dir.Delete(true);
        }
        catch
        { }
    }

Of course this will only delete it if the user has the name "Admin". Is there a way to make it work for all usernames? Plus I've noticed that this won't delete everything in this folder, is there a way to force delete or isn't that recommended?

Was it helpful?

Solution

To delete all files and folders in a folder ; use this code :

foreach (FileInfo file in TheDirectory.GetFiles())
{
   file.Delete(); 
}
foreach (DirectoryInfo dir in TheDirectory.GetDirectories())
{
   dir.Delete(true); 
}

On another stackoverflow thread I found this code to delete read-only files :

private static void DeleteFileSystemInfo(FileSystemInfo fsi)
{
fsi.Attributes = FileAttributes.Normal;
var di = fsi as DirectoryInfo;

if (di != null)
{
    foreach (var dirInfo in di.GetFileSystemInfos())
    {
        DeleteFileSystemInfo(dirInfo);
    }
}

fsi.Delete();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top