0

我想删除只读文件夹。我确实喜欢这个

  //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'”。

4

2 回答 2

2

正如我之前评论的那样,问题是您在删除文件时尝试删除文件夹。

您应该使用 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 {}
}
于 2013-04-23T07:10:30.960 回答
0

拒绝只读的原因可能有很多。文件夹在使用吗?在控制台中打开?运行可执行文件?所有这些事情你都应该检查。即使它有权限,如果该目录正在使用中,它也不允许你删除它。

于 2013-04-23T05:37:43.723 回答