Guest User

Untitled

a guest
Aug 12th, 2011
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.17 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.Diagnostics;
  7.  
  8. namespace ScratchPad
  9. {
  10.     class Program
  11.     {
  12.         static void RemoveLocalFiles1(List<string> LocalFiles)
  13.         {
  14.             // Ensure there is something to process
  15.             if (LocalFiles != null && LocalFiles.Count > 0)
  16.             {
  17.                 foreach (string file in LocalFiles)
  18.                 {
  19.                     try { File.Delete(file); }
  20.                     catch { }
  21.                 }
  22.             }
  23.         }
  24.  
  25.         // Option 2: Check for the existence of the file before delting
  26.         static void RemoveLocalFiles2(List<string> LocalFiles)
  27.         {
  28.             // Ensure there is something to process
  29.             if (LocalFiles != null && LocalFiles.Count > 0)
  30.             {
  31.                 foreach (string file in LocalFiles)
  32.                 {
  33.                     if (File.Exists(file) == true)
  34.                         File.Delete(file);
  35.                 }
  36.             }
  37.         }
  38.  
  39.         static void Main(string[] args)
  40.         {
  41.             List<string> files;
  42.             string filePath;
  43.             Stopwatch swatch = new Stopwatch();
  44.             int numFiles = 10000;
  45.  
  46.             files = new List<string>();
  47.             for (int i = 0; i < numFiles; i++)
  48.             {
  49.                 filePath = string.Format("file" + i + ".tmp");
  50.                 files.Add(filePath);
  51.             }
  52.             swatch.Reset();
  53.             swatch.Start();
  54.             RemoveLocalFiles1(files);
  55.             swatch.Stop();
  56.             Console.WriteLine("RemoveLocalFiles1 (try/catch) : " + swatch.ElapsedMilliseconds);
  57.  
  58.             files = new List<string>();
  59.             for (int i = 0; i < numFiles; i++)
  60.             {
  61.                 filePath = string.Format("file" + i + ".tmp");
  62.                 files.Add(filePath);
  63.             }
  64.             swatch.Reset();
  65.             swatch.Start();
  66.             RemoveLocalFiles2(files);
  67.             swatch.Stop();
  68.             Console.WriteLine("RemoveLocalFiles2 (try/catch) : " + swatch.ElapsedMilliseconds);
  69.  
  70.             Console.ReadLine();
  71.         }
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment