我想删除只读文件夹。我确实喜欢这个
//Remove Read-only for the Folder
File.SetAttributes(folderpath, File.GetAttributes(folderpath) & ~FileAttributes.ReadOnly);
//Delete Folder
FileInfo myfileinf = new FileInfo(folderpath);
myfileinf.Delete();
但是我得到了这个错误 “访问路径'E:\ Working Folder \ RPEssential \ RPEssential \ ResourcePlus-PL \ RDLReports \ t'被拒绝”。
答案 0 :(得分:1)
正如我之前评论的那样,问题是您在删除文件时尝试删除文件夹。
您应该使用Directory.Delete方法删除文件夹。
在以下链接中有一个关于如何使用它的好例子
http://msdn.microsoft.com/en-au/library/fxeahc5f(v=vs.100).aspx
public static void Main()
{
// Specify the directories you want to manipulate.
string path = @"c:\MyDir";
string subPath = @"c:\MyDir\temp";
try
{
// Determine whether the directory exists.
if (!Directory.Exists(path))
{
// Create the directory.
Directory.CreateDirectory(path);
}
if (!Directory.Exists(subPath))
{
// Create the directory.
Directory.CreateDirectory(subPath);
}
// This will succeed because subdirectories are being deleted.
Console.WriteLine("I am about to attempt to delete {0}", path);
Directory.Delete(path, true);
Console.WriteLine("The Delete operation was successful.");
}
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}
finally {}
}
答案 1 :(得分:0)
只有被拒绝的原因可能有很多。该文件夹正在使用中吗?在控制台中打开?运行可执行文件?你应该检查所有这些事情。即使它具有权限,如果目录正在使用中,也不允许您删除它。