Advertisement
Guest User

Untitled

a guest
Mar 2nd, 2010
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.30 KB | None | 0 0
  1. namespace Editor.IO
  2. {
  3.     using System;
  4.     using System.IO;
  5.     using System.Windows.Forms;
  6.  
  7.  
  8.     public class TempHelper : IDisposable
  9.     {
  10.         private string m_baseDirectory;
  11.         private string m_buildDirectory;
  12.         private string m_addendumDirectory;
  13.  
  14.         public TempHelper(string addendum)
  15.         {
  16.             m_addendumDirectory = addendum;
  17.  
  18.             CreateTempDirectory();
  19.         }
  20.  
  21.         public string TempDirectory
  22.         {
  23.             get { return m_buildDirectory; }
  24.             set { m_buildDirectory = value; }
  25.         }
  26.  
  27.         /// <summary>
  28.         /// Creates a temporary directory in which to build content.
  29.         /// </summary>
  30.         private void CreateTempDirectory()
  31.         {
  32.             // Start with a standard base name:
  33.             //
  34.             //  %temp%\Editor.exe
  35.  
  36.             m_baseDirectory = Path.Combine(Path.GetTempPath(), Path.GetFileName(Application.ExecutablePath));
  37.  
  38.             // Include our process ID, in case there is more than
  39.             // one copy of the program running at the same time:
  40.             //
  41.             //  %temp%\Editor.exe\<ProcessId>
  42.  
  43.             //int processId = Process.GetCurrentProcess().Id;
  44.             //string processDirectory = Path.Combine(baseDirectory, processId.ToString());
  45.  
  46.             // Include a salt value, in case the program
  47.             // creates more than one ContentBuilder instance:
  48.             //
  49.             //  %temp%\Editor.exe\<ProcessId>\<Salt>
  50.  
  51.             //directorySalt++;
  52.  
  53.             //buildDirectory = Path.Combine(processDirectory, directorySalt.ToString());
  54.             if (!string.IsNullOrEmpty(m_addendumDirectory))
  55.                 m_buildDirectory = Path.Combine(m_baseDirectory, m_addendumDirectory);
  56.             else
  57.                 m_buildDirectory = m_baseDirectory;
  58.  
  59.             // Create our temporary directory.
  60.             Directory.CreateDirectory(m_buildDirectory);
  61.  
  62.             PurgeTempDirectory();
  63.         }
  64.  
  65.         /// <summary>
  66.         /// Deletes our temporary directory when we are finished with it.
  67.         /// </summary>
  68.         private void DeleteTempDirectory()
  69.         {
  70.             Directory.Delete(TempDirectory, true);
  71.         }
  72.  
  73.         private void PurgeTempDirectory()
  74.         {
  75.             // Check all subdirectories of our base location.
  76.             foreach (string directory in Directory.GetDirectories(m_buildDirectory))
  77.             {
  78.                 Directory.Delete(directory, true);
  79.             }
  80.         }
  81.  
  82.         #region Implementation of IDisposable
  83.  
  84.         public void Dispose()
  85.         {
  86.             Dispose(true);
  87.             GC.SuppressFinalize(this);
  88.         }
  89.  
  90.         private void Dispose(bool disposing)
  91.         {
  92.             if (disposing)
  93.             {
  94.                 DeleteTempDirectory();
  95.             }
  96.         }
  97.  
  98.         #endregion
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement