【发布时间】:2015-01-01 15:04:54
【问题描述】:
您好,我基本上是想在我的应用程序中创建一个按钮,该按钮可以删除我的 %appdata% 文件夹,但是它一直说无法删除只读文件,所以我决定做一些谷歌搜索,但问题仍然存在这是我最近的尝试仍然没有任何线索?
我要删除的是 %appdata%/test,它也有子文件夹。
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);
}
【问题讨论】:
-
这个方法的结果是什么?
-
@commusoft i.imgur.com/wdBeCH3.png
标签: c#