Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO;
- using System.Diagnostics;
- namespace ScratchPad
- {
- class Program
- {
- static void RemoveLocalFiles1(List<string> LocalFiles)
- {
- // Ensure there is something to process
- if (LocalFiles != null && LocalFiles.Count > 0)
- {
- foreach (string file in LocalFiles)
- {
- try { File.Delete(file); }
- catch { }
- }
- }
- }
- // Option 2: Check for the existence of the file before delting
- static void RemoveLocalFiles2(List<string> LocalFiles)
- {
- // Ensure there is something to process
- if (LocalFiles != null && LocalFiles.Count > 0)
- {
- foreach (string file in LocalFiles)
- {
- if (File.Exists(file) == true)
- File.Delete(file);
- }
- }
- }
- static void Main(string[] args)
- {
- List<string> files;
- string filePath;
- Stopwatch swatch = new Stopwatch();
- int numFiles = 10000;
- files = new List<string>();
- for (int i = 0; i < numFiles; i++)
- {
- filePath = string.Format("file" + i + ".tmp");
- files.Add(filePath);
- }
- swatch.Reset();
- swatch.Start();
- RemoveLocalFiles1(files);
- swatch.Stop();
- Console.WriteLine("RemoveLocalFiles1 (try/catch) : " + swatch.ElapsedMilliseconds);
- files = new List<string>();
- for (int i = 0; i < numFiles; i++)
- {
- filePath = string.Format("file" + i + ".tmp");
- files.Add(filePath);
- }
- swatch.Reset();
- swatch.Start();
- RemoveLocalFiles2(files);
- swatch.Stop();
- Console.WriteLine("RemoveLocalFiles2 (try/catch) : " + swatch.ElapsedMilliseconds);
- Console.ReadLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment