Hello I'm basically trying to make a button in my application which can delete my %appdata% folder but there's a problem it keeps saying cannot delete a read only file so i decided to do some googling but the problem still continues anyways here's my latest try still didn't work any clue?
What I'm trying to delete is %appdata%/test which also has sub folders.
private void ClearButton_OnClick(object sender, RoutedEventArgs e)
{
string filepath = (Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "test"));
//Get Currently Applied Access Control
FileSecurity fileS = File.GetAccessControl(filepath);
//Update it, Grant Current User Full Control
SecurityIdentifier cu = WindowsIdentity.GetCurrent().User;
fileS.SetOwner(cu);
fileS.SetAccessRule(new FileSystemAccessRule(cu, FileSystemRights.FullControl, AccessControlType.Allow));
//Update the Access Control on the File
File.SetAccessControl(filepath, fileS);
//Delete the file
File.Delete(filepath);
Process.Start(Application.ResourceAssembly.Location);
Environment.Exit(0);
}