DirectoryInfo.Delete vs Directory.Delete

前端 未结 2 2066
野性不改
野性不改 2020-12-21 17:39

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:

相关标签:
2条回答
  • 2020-12-21 18:13

    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);
        }
    
    0 讨论(0)
  • 2020-12-21 18:30

    The first code sample will only delete the folders "C:\Users\user\Desktop\1" and "C:\Users\user\Desktop\2" regardless of what is passed in the parameter.

    The second code sample will delete all files and folders that are inside the directory specified by the parameter.

    0 讨论(0)
提交回复
热议问题