有个文件夹是只读的,里面的文件也是只读的,现在想删除这个文件夹,并删除里面的文件new FileInfo(DirName).IsReadOnly = false;
或者
new FileInfo(DirName).Attributes = FileAttributes.Normal;现在用上面的两个函数,再调用Directory.Delete(DirName, true);
还是抛出"xxxx文件拒绝访问"异常,怎么解决?

解决方案 »

  1.   

    删除文件前先去掉文件的只读属性:new FileInfo(@"E:\001.txt").IsReadOnly = false;
      

  2.   


    (new FileInfo(@"E:\001.txt")).IsReadOnly = false;
      

  3.   

    那就先去掉文件夹的只读属性http://topic.csdn.net/t/20040514/14/3067523.html
      

  4.   


    现在问题是:现在只能遍历文件,然后去掉文件的只读属性,删掉文件,然后再删掉目录,
    有没有一劳永逸的方法:一次性删除文件和文件夹,就像 rd /S /D xxx 这个命令一样干脆利索。。
      

  5.   

    那就直接用c# 写cmd命令好了
      

  6.   

    直接给命令 ok..
    public void Cmdshutdown(string cmdstr)
            {
                Process p = new Process();
                p.StartInfo.FileName = "cmd.exe";
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.RedirectStandardInput = true;
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.RedirectStandardError = true;
                p.StartInfo.CreateNoWindow = true;
                p.Start();
                p.StandardInput.WriteLine(cmdstr);
                p.StandardInput.WriteLine("exit");
                string strRst = p.StandardOutput.ReadToEnd();
                p.WaitForExit();        }