开发者

deleting entire folder including all the files inside

The following is what i have when i try to delete a folder:

namespace sortfolder
{
    class Program
    {
        static string path = "C:\\Work\\6.70_Extensions\\NightlyBuild\\";

        static void Main(string[] args)
        {
            var di = new DirectoryInfo("C:\\Work\\6.70_Extensions\\NightlyBuild");

            foreach (var file in di.GetFiles("*", SearchOption.AllDirectories))
                file.Attributes &= ~FileAttributes.ReadOnly;

            var files = Dire开发者_StackOverflow中文版ctory.GetDirectories(path, "SASE Lab Tools.*");
            foreach(var file in files)
            Console.WriteLine(file);
            foreach(var file in files.OrderByDescending(x=>x).Skip(7))
            Console.WriteLine(file);
            foreach(var file in files.OrderByDescending(x=>x).Skip(7))
            Directory.Delete(file);
        }
    }
}

As mentioned in the title, i would like to delete this folder along with all its content. How do i go about doing it? Apparently i have IOException @ Directory.Delete(file) as it contains files


You missing true Directory.Delete(file,true);
For example

public static void Main() 
    {
        string path = @"c:\MyDir\temp";
        try 
        {
            Directory.Delete(path, true);
        } 
        catch (Exception e) 
        {
            Console.WriteLine("The process failed: {0}", e.ToString());
        } 
        finally {}
    }


Directory.Delete has a second parameter which takes a bool = recrusive. Use that overload instead.

http://msdn.microsoft.com/en-us/library/fxeahc5f.aspx

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜