DirectoryInfo.Delete VS Directory.Delete [英] DirectoryInfo.Delete vs Directory.Delete

查看:403
本文介绍了DirectoryInfo.Delete VS Directory.Delete的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要删除一些临时文件,所以我工作的小程序,删除它们为我的内容。我有两个code样品,但我很困惑,:

  1. 在其中code样品比较好?
  2. 的第一样品,code1,删除这些文件1和2,但在第二个样品,code2将删除包含夹1和2?

code1

 公共无效DeleteContains(字符串Pathz)
    {
        名单< D​​irectoryInfo的> FolderToClear =新的名单,其中,DirectoryInfo的>();
        FolderToClear.Add(新的DirectoryInfo(@C:\用户\用户\桌面\ 1));
        FolderToClear.Add(新的DirectoryInfo(@C:\用户\用户\桌面\ 2));

        的foreach(DirectoryInfo的中X FolderToClear)
        {
            x.Delete(真正的);
        }
    }
 

code 2

 私人无效DeleteContents(字符串路径)
    {
        字符串[] DirectoryList = Directory.GetDirectories(路径);
        字符串[]文件列表= Directory.GetFiles(路径);

        的foreach(在文件列表字符串的文件)
        {
            File.Delete(文件);
        }
        的foreach(字符串directoryin DirectoryList)
        {
            Directory.Delete(目录,真正的);
        }
    }
 

解决方案

编辑:我相信OP希望DirectoryInfo.Delete和Directory.Delete的比较。

如果你看一下反编译源每种方法(我用的ReSharper的告诉我),你可以看到DirectoryInfo.Delete和Directory.Delete有4个参数都调用Delete方法。 IMHO,唯一的区别是,Directory.Delete必须调用Path.GetFullPathInternal以获得完整路径。 Path.GetFullPathInternal其实是一个很长的方法有很多检查。没有做了一系列的测试性能,这将是不可能的,以确定这是更快并且由多少

Directory.Delete

  [ResourceExposure(ResourceScope.Machine)
    [ResourceConsumption(ResourceScope.Machine)
    公共静态无效删除(字符串路径,布尔递归)
    {
        字符串FULLPATH = Path.GetFullPathInternal(路径);
        删除(完整路径,路径,递归的,真正的);
    }
 

DirectoryInfo.Delete

  [ResourceExposure(ResourceScope.None)
    [ResourceConsumption(ResourceScope.Machine,ResourceScope.Machine)
    公共无效删除(布尔递归)
    {
        Directory.Delete(FULLPATH,OriginalPath,递归的,真正的);
    }
 

I want to delete the contents of some temp files so I am working on small program that deletes them for me. I have these two code samples but I'm confused as to:

  1. Which code sample is better?
  2. The first sample, code1, deletes the files 1 and 2 but the second sample, code2 will delete the contains of folder 1 and 2?

code1

    public void DeleteContains(string Pathz)
    {
        List<DirectoryInfo> FolderToClear = new List<DirectoryInfo>();
        FolderToClear.Add(new DirectoryInfo(@"C:\Users\user\Desktop\1"));
        FolderToClear.Add(new DirectoryInfo(@"C:\Users\user\Desktop\2"));

        foreach (DirectoryInfo x in FolderToClear)
        {
            x.Delete(true);
        }
    }

code 2

    private void DeleteContents(string Path)
    {
        string[] DirectoryList = Directory.GetDirectories(Path);
        string[] FileList = Directory.GetFiles(Path);

        foreach (string file in FileList)
        {
            File.Delete(file);
        }
        foreach ( string directoryin DirectoryList)
        {
            Directory.Delete(directory, true);
        }
    }

解决方案

EDIT: I believe the OP wants a comparison of DirectoryInfo.Delete and Directory.Delete.

If you look at the decompiled source for each method (I used resharper to show me), you can see that DirectoryInfo.Delete and Directory.Delete both call the Delete method with 4 arguments. IMHO, the only difference is that Directory.Delete has to call Path.GetFullPathInternal to get the fullpath. Path.GetFullPathInternal is actually a very long method with lots of checks. Without doing a series of tests for performance, it would be unlikely to determine which is faster and by how much.

Directory.Delete

    [ResourceExposure(ResourceScope.Machine)]
    [ResourceConsumption(ResourceScope.Machine)]
    public static void Delete(String path, bool recursive)
    { 
        String fullPath = Path.GetFullPathInternal(path);
        Delete(fullPath, path, recursive, true); 
    } 

DirectoryInfo.Delete

    [ResourceExposure(ResourceScope.None)] 
    [ResourceConsumption(ResourceScope.Machine, ResourceScope.Machine)]
    public void Delete(bool recursive) 
    {
        Directory.Delete(FullPath, OriginalPath, recursive, true);
    }

这篇关于DirectoryInfo.Delete VS Directory.Delete的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆