【发布时间】:2016-01-11 03:13:27
【问题描述】:
我正在编写一个程序来从我的电脑中删除一个文件或文件夹,但我似乎无法获得删除它的所有权限。
是否有办法从文件/文件夹中删除所有权限,以使其可以被删除?
我的代码是:
//for each file in my filepath
foreach (string ficheiro in caminhoFicheiros)
{
string deletar = "Deleting: ";
meuProgresso.Visible = true;
meuProgresso.Increment(1);
listBox.Items.Add(deletar + ficheiro.Substring(tamanho, ficheiro.Length - tamanho));
//tamanho do ficheiro
long length = new System.IO.FileInfo(ficheiro).Length;
//lbMostrarItems.SelectedIndex = lbMostrarItems.Items.Count - 1;
// instead of doing a try catch, i want to be able to delete the file
// just by getting users permission.
try
{
File.Delete(ficheiro);
listBox.Items.Add("Deleted with sucess!");
}
catch (IOException)
{
Thread.Sleep(0);
}
// i can´t delete the folder because i don´t have permissions.
catch (UnauthorizedAccessException)
{
File.Delete(ficheiro);
}
somarTamanho += length;
}
我只想获得用户权限并能够删除具有该权限的文件。
【问题讨论】:
-
您是否尝试过向项目添加清单并像 Windows 一样请求管理员权限?
-
如果你可以从代码中覆盖权限,那就有点傻了。
-
哦,我没想到,我会看看我能不能做到
标签: c# delete-file user-permissions