3

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);
        }
    }
xXghostXx
  • 67
  • 2
  • 6
  • What exactly are you trying to do? Delete whatever is in the folder? – cheesemacfly Dec 14 '12 at 19:11
  • 3
    Instead of naming variables like `Fil` and `Drectory`, try `file` and `directory`. It makes 9/10 people happier. –  Dec 14 '12 at 19:13
  • (I changed the title. Now it's your turn to change the code to make them "equivalent" in how they use the input or otherwise explain why/how it matters.) –  Dec 14 '12 at 19:16
  • @cheesemacfly thanks and done.. what I am trying to do is deleting the MSN cashes and windows temp files using this small code... I think both codes are not useful i think there is something better... hope u suggest something thanks again :) – xXghostXx Dec 14 '12 at 19:39
  • @xXghostXx can you describe what you mean by "something better". A better library? A better way to delete the cache? A faster way to delete? – AboutDev Dec 14 '12 at 19:44
  • @AboutDev A better way to delete the caches – xXghostXx Dec 14 '12 at 19:50
  • @AboutDev the the fist code will allow me to add as much as I want paths but the second one will not allow me to add paths or I have to create array of string for each path anyway better to do it ?? like to use the fist code to delete the contains of the folder NOT the folder and its contain – xXghostXx Dec 14 '12 at 20:14
  • I think you are close to the "right" solution with Code 2 see [this link](http://stackoverflow.com/questions/1288718/how-to-delete-all-files-and-folders-in-a-directory). Next step is catching the exceptions. – cheesemacfly Dec 14 '12 at 20:17
  • @cheesemacfly but the second one is not really useful because I have to create new array for each new path and new loops which is something painful ???? anyway better ??? – xXghostXx Dec 14 '12 at 20:34
  • You have no choice but to list what you want to delete...you don't need to create a string[] if this is the issue you have. Simply use a foreach like this: `foreach (string file in Directory.GetFiles(Path)){ File.Delete(file);}` – cheesemacfly Dec 14 '12 at 20:43
  • ummmm waitting in hope that there is a better way... but I am thinking using the both code and ill add path depending on the folder if I am able to delete it or not... but still waiting guys any ideas ? – xXghostXx Dec 14 '12 at 21:05

2 Answers2

2

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);
    }
AboutDev
  • 1,923
  • 1
  • 23
  • 33
  • thanks what I am trying to do is deleting the MSN cashes and windows temp files using this small code and maybe in future i will add cookies files for browsers but I am working on it step by step just to teach my self thinks I had leaned while doing small codes and develop it ... I think both codes are not useful or fixable enough i think there is something better... hope u suggest something thanks again – xXghostXx Dec 14 '12 at 19:44
  • the the fist code will allow me to add as much as I want paths but the second one will not allow me to add paths or I have to create array of string for each path anyway better to do it ?? like to use the fist code to delete the contains of the folder NOT the folder and its contain – xXghostXx Dec 14 '12 at 20:13
  • What @cheesemacfly said in the comments above is correct. You can also use the Parallel libraries in order to delete things faster. See http://stackoverflow.com/questions/4981410/what-is-the-fastest-way-of-deleting-files-in-a-directory-except-specific-file for more info. – AboutDev Dec 14 '12 at 22:44
0

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.

Scott Chapman
  • 920
  • 1
  • 7
  • 13
  • yea I know man I wrote that in my question but anyway to let the code1 to do whats code 2 do ?? – xXghostXx Dec 14 '12 at 19:47
  • the the fist code will allow me to add as much as I want paths but the second one will not allow me to add paths or I have to create array of string for each path anyway better to do it ?? like to use the fist code to delete the contains of the folder NOT the folder and its contain – xXghostXx Dec 14 '12 at 20:13