mscorlib(4.0.0.0) API with additions
TempFileCollection.cs
1 using System.Collections;
2 using System.IO;
6 
8 {
10  [Serializable]
11  [PermissionSet(SecurityAction.LinkDemand, Name = "FullTrust")]
12  [PermissionSet(SecurityAction.InheritanceDemand, Name = "FullTrust")]
14  {
15  private string basePath;
16 
17  private string tempDir;
18 
19  private bool keepFiles;
20 
21  private Hashtable files;
22 
25  public int Count => files.Count;
26 
29  int ICollection.Count
30  {
31  get
32  {
33  return files.Count;
34  }
35  }
36 
39  object ICollection.SyncRoot
40  {
41  get
42  {
43  return null;
44  }
45  }
46 
51  {
52  get
53  {
54  return false;
55  }
56  }
57 
60  public string TempDir
61  {
62  get
63  {
64  if (tempDir != null)
65  {
66  return tempDir;
67  }
68  return string.Empty;
69  }
70  }
71 
75  public string BasePath
76  {
77  get
78  {
79  EnsureTempNameCreated();
80  return basePath;
81  }
82  }
83 
87  public bool KeepFiles
88  {
89  get
90  {
91  return keepFiles;
92  }
93  set
94  {
95  keepFiles = value;
96  }
97  }
98 
101  : this(null, keepFiles: false)
102  {
103  }
104 
107  public TempFileCollection(string tempDir)
108  : this(tempDir, keepFiles: false)
109  {
110  }
111 
116  public TempFileCollection(string tempDir, bool keepFiles)
117  {
118  this.keepFiles = keepFiles;
119  this.tempDir = tempDir;
121  }
122 
124  void IDisposable.Dispose()
125  {
126  Dispose(disposing: true);
127  GC.SuppressFinalize(this);
128  }
129 
133  protected virtual void Dispose(bool disposing)
134  {
135  Delete();
136  }
137 
140  {
141  Dispose(disposing: false);
142  }
143 
147  public string AddExtension(string fileExtension)
148  {
149  return AddExtension(fileExtension, keepFiles);
150  }
151 
159  public string AddExtension(string fileExtension, bool keepFile)
160  {
161  if (fileExtension == null || fileExtension.Length == 0)
162  {
163  throw new ArgumentException(SR.GetString("InvalidNullEmptyArgument", "fileExtension"), "fileExtension");
164  }
165  string text = BasePath + "." + fileExtension;
166  AddFile(text, keepFile);
167  return text;
168  }
169 
177  public void AddFile(string fileName, bool keepFile)
178  {
179  if (fileName == null || fileName.Length == 0)
180  {
181  throw new ArgumentException(SR.GetString("InvalidNullEmptyArgument", "fileName"), "fileName");
182  }
183  if (files[fileName] != null)
184  {
185  throw new ArgumentException(SR.GetString("DuplicateFileName", fileName), "fileName");
186  }
187  files.Add(fileName, keepFile);
188  }
189 
193  {
194  return files.Keys.GetEnumerator();
195  }
196 
200  {
201  return files.Keys.GetEnumerator();
202  }
203 
207  void ICollection.CopyTo(Array array, int start)
208  {
209  files.Keys.CopyTo(array, start);
210  }
211 
215  public void CopyTo(string[] fileNames, int start)
216  {
217  files.Keys.CopyTo(fileNames, start);
218  }
219 
220  private void EnsureTempNameCreated()
221  {
222  if (basePath == null)
223  {
224  string text = null;
225  bool flag = false;
226  int num = 5000;
227  do
228  {
229  try
230  {
231  basePath = GetTempFileName(TempDir);
232  string fullPath = Path.GetFullPath(basePath);
233  new FileIOPermission(FileIOPermissionAccess.AllAccess, fullPath).Demand();
234  text = basePath + ".tmp";
235  FileStream fileStream;
236  using (fileStream = new FileStream(text, FileMode.CreateNew, FileAccess.Write))
237  {
238  }
239  flag = true;
240  }
241  catch (IOException e)
242  {
243  num--;
244  uint num2 = 2147942480u;
245  if (num == 0 || Marshal.GetHRForException(e) != num2)
246  {
247  throw;
248  }
249  flag = false;
250  }
251  }
252  while (!flag);
253  files.Add(text, keepFiles);
254  }
255  }
256 
257  private bool KeepFile(string fileName)
258  {
259  object obj = files[fileName];
260  if (obj == null)
261  {
262  return false;
263  }
264  return (bool)obj;
265  }
266 
268  public void Delete()
269  {
270  if (files == null || files.Count <= 0)
271  {
272  return;
273  }
274  string[] array = new string[files.Count];
275  files.Keys.CopyTo(array, 0);
276  string[] array2 = array;
277  foreach (string text in array2)
278  {
279  if (!KeepFile(text))
280  {
281  Delete(text);
282  files.Remove(text);
283  }
284  }
285  }
286 
287  internal void SafeDelete()
288  {
289  WindowsImpersonationContext impersonation = Executor.RevertImpersonation();
290  try
291  {
292  Delete();
293  }
294  finally
295  {
296  Executor.ReImpersonate(impersonation);
297  }
298  }
299 
300  private void Delete(string fileName)
301  {
302  try
303  {
304  File.Delete(fileName);
305  }
306  catch
307  {
308  }
309  }
310 
311  private static string GetTempFileName(string tempDir)
312  {
313  if (string.IsNullOrEmpty(tempDir))
314  {
315  tempDir = Path.GetTempPath();
316  }
317  string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(Path.GetRandomFileName());
318  if (tempDir.EndsWith("\\", StringComparison.Ordinal))
319  {
320  return tempDir + fileNameWithoutExtension;
321  }
322  return tempDir + "\\" + fileNameWithoutExtension;
323  }
324  }
325 }
object SyncRoot
Gets an object that can be used to synchronize access to the T:System.Collections....
Definition: ICollection.cs:23
Provides command execution functions for invoking compilers. This class cannot be inherited.
Definition: Executor.cs:18
FileIOPermissionAccess
Specifies the type of file access requested.
virtual void Add(object key, object value)
Adds an element with the specified key and value into the T:System.Collections.Hashtable.
Definition: Hashtable.cs:916
static int GetHRForException(Exception e)
Converts the specified exception to an HRESULT.
static void SuppressFinalize(object obj)
Requests that the common language runtime not call the finalizer for the specified object.
Definition: GC.cs:308
StringComparison
Specifies the culture, case, and sort rules to be used by certain overloads of the M:System....
string AddExtension(string fileExtension, bool keepFile)
Adds a file name with the specified file name extension to the collection, using the specified value ...
FileMode
Specifies how the operating system should open a file.
Definition: FileMode.cs:8
Provides a mechanism for releasing unmanaged resources.To browse the .NET Framework source code for t...
Definition: IDisposable.cs:8
Definition: __Canon.cs:3
TempFileCollection()
Initializes a new instance of the T:System.CodeDom.Compiler.TempFileCollection class with default val...
TempFileCollection(string tempDir)
Initializes a new instance of the T:System.CodeDom.Compiler.TempFileCollection class using the specif...
TempFileCollection(string tempDir, bool keepFiles)
Initializes a new instance of the T:System.CodeDom.Compiler.TempFileCollection class using the specif...
Exposes an enumerator, which supports a simple iteration over a non-generic collection....
Definition: IEnumerable.cs:9
SecurityAction
Specifies the security actions that can be performed using declarative security.
int Count
Gets the number of files in the collection.
bool KeepFiles
Gets or sets a value indicating whether to keep the files, by default, when the M:System....
Represents a collection of key/value pairs that are organized based on the hash code of the key....
Definition: Hashtable.cs:17
virtual ICollection Keys
Gets an T:System.Collections.ICollection containing the keys in the T:System.Collections....
Definition: Hashtable.cs:617
Provides a T:System.IO.Stream for a file, supporting both synchronous and asynchronous read and write...
Definition: FileStream.cs:15
The exception that is thrown when an I/O error occurs.
Definition: IOException.cs:10
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition: Array.cs:17
bool IsSynchronized
Gets a value indicating whether access to the T:System.Collections.ICollection is synchronized (threa...
Definition: ICollection.cs:33
void CopyTo(string[] fileNames, int start)
Copies the members of the collection to the specified string, beginning at the specified index.
Provides a collection of methods for allocating unmanaged memory, copying unmanaged memory blocks,...
Definition: Marshal.cs:15
static string GetFullPath(string path)
Returns the absolute path for the specified path string.
Definition: Path.cs:446
IEnumerator GetEnumerator()
Returns an enumerator that iterates through a collection.
Controls the system garbage collector, a service that automatically reclaims unused memory.
Definition: GC.cs:11
The exception that is thrown when one of the arguments provided to a method is not valid.
void Demand()
Forces a T:System.Security.SecurityException at run time if all callers higher in the call stack have...
virtual void Dispose(bool disposing)
Releases the unmanaged resources used by the T:System.CodeDom.Compiler.TempFileCollection and optiona...
string TempDir
Gets the temporary directory to store the temporary files in.
FileAccess
Defines constants for read, write, or read/write access to a file.
Definition: FileAccess.cs:9
virtual void Remove(object key)
Removes the element with the specified key from the T:System.Collections.Hashtable.
Definition: Hashtable.cs:1349
string BasePath
Gets the full path to the base file name, without a file name extension, on the temporary directory p...
static string GetTempPath()
Returns the path of the current user's temporary folder.
Definition: Path.cs:976
Specifies that the class can be serialized.
IEnumerator GetEnumerator()
Gets an enumerator that can enumerate the members of the collection.
void AddFile(string fileName, bool keepFile)
Adds the specified file to the collection, using the specified value indicating whether to keep the f...
static StringComparer OrdinalIgnoreCase
Gets a T:System.StringComparer object that performs a case-insensitive ordinal string comparison.
Provides static methods for the creation, copying, deletion, moving, and opening of a single file,...
Definition: File.cs:14
static void Delete(string path)
Deletes the specified file.
Definition: File.cs:324
int Count
Gets the number of elements contained in the T:System.Collections.ICollection.
Definition: ICollection.cs:14
Controls the ability to access files and folders. This class cannot be inherited.
static string GetRandomFileName()
Returns a random folder name or file name.
Definition: Path.cs:997
Defines size, enumerators, and synchronization methods for all nongeneric collections.
Definition: ICollection.cs:8
void CopyTo(Array array, int index)
Copies the elements of the T:System.Collections.ICollection to an T:System.Array, starting at a parti...
void Dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resourc...
Supports a simple iteration over a non-generic collection.
Definition: IEnumerator.cs:9
Represents a collection of temporary files.
Performs operations on T:System.String instances that contain file or directory path information....
Definition: Path.cs:13
string AddExtension(string fileExtension)
Adds a file name with the specified file name extension to the collection.
void Delete()
Deletes the temporary files within this collection that were not marked to be kept.
virtual int Count
Gets the number of key/value pairs contained in the T:System.Collections.Hashtable.
Definition: Hashtable.cs:658
static string GetFileNameWithoutExtension(string path)
Returns the file name of the specified path string without the extension.
Definition: Path.cs:939
Represents a string comparison operation that uses specific case and culture-based or ordinal compari...
Represents the Windows user prior to an impersonation operation.