【问题标题】:How to delete Read only folder in asp.net ,c#如何删除asp.net中的只读文件夹,c#
【发布时间】:2013-04-23 05:22:09
【问题描述】:

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

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

【问题讨论】:

  • 与您的 ASP.NET 应用程序池一起运行的用户必须具有删除该文件夹的适当权限。希望这会有所帮助
  • @juanreyesv 但是管理员已经设置了权限
  • 尝试手动删除只读标志并执行代码。是否有效?
  • @Sachin 如果你想删除整个文件夹,你应该使用 Directory.Delete method

标签: asp.net


【解决方案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 {}
}

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-15
      • 2010-09-20
      • 2018-04-19
      • 2011-07-29
      • 2010-10-11
      • 2010-10-18
      相关资源
      最近更新 更多