andrew4582

TempFileManager

Jul 28th, 2012
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.34 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Reflection;
  4.  
  5. namespace System.IO
  6. {
  7.     /*
  8.     //Example
  9.     //creates temp folder in User/Temp with the assembly name
  10.     using(TempFileManager tmgr = new TempFileManager(".txt")) {
  11.         File.WriteAllText(tmgr.GetTempFilename(),"Test");
  12.  
  13.     }//deletes temp folder upon Dispose()
  14.  
  15.      * */
  16.     /// <summary>
  17.     /// Creates and cleans up a temp folder and temp filenames.
  18.     /// </summary>
  19.     public class TempFileManager:IDisposable
  20.     {
  21.         string _extension;
  22.         string _folder;
  23.         static int _counter = 0;
  24.  
  25.         /// <summary>
  26.         /// Creates a folder with the entry assembly's name in the user's Temp folder
  27.         /// and creates temp files with a .tmp extension.
  28.         /// </summary>
  29.         /// <remarks>T</remarks>
  30.         public TempFileManager() : this(".temp") { }
  31.  
  32.         /// <summary>
  33.         /// Creates a folder with the name &lt;ApplicationName&gt;Temp in the user's Temp folder
  34.         /// and creates temp files with the given extension.
  35.         /// </summary>
  36.         public TempFileManager(string extension) : this(GetAssemblyName(),extension) { }
  37.  
  38.         /// <summary>
  39.         /// Creates a folder with the given name in the user's Temp folder
  40.         /// and creates temp files the given extension.
  41.         /// </summary>
  42.         public TempFileManager(string folderName,string extension)
  43.         {
  44.             _folder = Path.Combine(Path.GetTempPath(),folderName);
  45.             _extension = extension;
  46.  
  47.             Directory.CreateDirectory(_folder);
  48.         }
  49.  
  50.         static string GetAssemblyName()
  51.         {
  52.             return Assembly.GetEntryAssembly().GetName().Name;
  53.         }
  54.  
  55.         /// <summary>
  56.         /// Gets a temp filename.
  57.         /// </summary>
  58.         /// <returns>A string containing the temp filename.</returns>
  59.         public string GetTempFilename()
  60.         {
  61.             return GetTempFilename(_extension);
  62.         }
  63.  
  64.         /// <summary>
  65.         /// Gets a temp filename.  
  66.         /// </summary>
  67.         /// <param name="extension">The extension for the temp file.</param>
  68.         /// <returns>A string containing the temp filename.</returns>
  69.         public string GetTempFilename(string extension)
  70.         {
  71.             return Path.Combine(_folder,_counter++ + extension);
  72.         }
  73.  
  74.         /// <summary>
  75.         /// Deletes the temp folder and all files within.
  76.         /// </summary>
  77.         /// <param name="throwException">Indicates if any exceptions occurring while deleting should
  78.         /// be thrown or suppressed.</param>
  79.         public void DeleteFolder(bool throwException)
  80.         {
  81.             try
  82.             {
  83.                 Directory.Delete(_folder,true);
  84.             }
  85.             catch
  86.             {
  87.                 if(throwException)
  88.                 {
  89.                     throw;
  90.                 }
  91.             }
  92.         }
  93.  
  94.         /// <summary>
  95.         /// Deletes the temp folder and all files within.  Any exceptions thrown are suppressed.  
  96.         /// If you want to handle any exceptions, call DeleteFolder(true);
  97.         /// </summary>
  98.         public void Dispose()
  99.         {
  100.             GC.SuppressFinalize(this);
  101.             DeleteFolder(false);
  102.         }
  103.  
  104.         ~TempFileManager()
  105.         {
  106.             DeleteFolder(false);
  107.         }
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment