删除目录访问被拒绝c# [英] delete directory access denied c#

查看:89
本文介绍了删除目录访问被拒绝c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我想从目录中删除目录和所有文件,所以我先编写代码我要从目录中删除文件然后删除目录但是给出错误 访问路径'D:\\Deepu \\ Verma \\DirectoryFolder'被拒绝。

和我的代码是



  if (System.IO .Directory.Exists(path))
{
foreach var item < span class =code-keyword> in System.IO.Directory.GetFiles(path))
{
System.IO.File.Delete(path);
}
System.IO.Directory.Delete(path);
}





请让我现在解决。

解决方案

可能您没有对该文件夹的删除权限。

使用所有用户的完全权限创建它,或者更改父文件夹的权限并应用于所有子目录。



请记住,您的应用可能没有以同一个用户身份运行:因此应用于该文件夹的权限可能与您打开Windows资源管理器时的权限不同。

感谢您的帮助,现在它的工作正常。代码几乎没有变化,更新代码是:



 如果 (System.IO.Directory.Exists(path))
{
foreach var item in System.IO.Directory.GetFiles(path))
{
System.IO.File.Delete(item);
}
System.IO.Directory.Delete(path);
}


Hi,

I want to delete directory and all file from a directory so for that I written code to first I am deleting file from directory and then directory but its giving error "Access to the path 'D:\\Deepu\\Verma\\DirectoryFolder' is denied."
and my code is

if (System.IO.Directory.Exists(path))
{
foreach (var item in System.IO.Directory.GetFiles(path))
{
System.IO.File.Delete(path);
}
System.IO.Directory.Delete(path);
}



Please let me now solution.

解决方案

Probably, you don't have delete permissions on the folder.
Either create it with full permissions for all users, or change the permissions on the parent folder and apply to all subdirectories.

Remember that your app may not be running as the same user you are: so the permissions that apply to the folder may not be the same as when you open Windows Explorer.


Thanks for helping me, now its working fine. There was little change in code and update code is:

if (System.IO.Directory.Exists(path))
{
    foreach (var item in System.IO.Directory.GetFiles(path))
    {
        System.IO.File.Delete(item);
    }
    System.IO.Directory.Delete(path);
}


这篇关于删除目录访问被拒绝c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆