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:
- Which code sample is better?
- 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);
}
}