我正在写一个程序从我的电脑中删除一个文件或文件夹,但似乎我无法获得删除它的所有权限。我如何获得管理员权限以删除文件/文件夹?
无论如何删除文件/文件夹的所有权限,使其可能被删除?
我的代码是:
//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那样的管理员权限? –
如果你可以重写代码的权限,这将会有点愚蠢。 – Jamiec
ohh我没有想到这一点,我会看看我是否可以做到这一点 –