TempFileCollection
using System.Collections;
namespace System.IO.Internal
{
internal sealed class TempFileCollection : ICollection, IEnumerable, IDisposable
{
private string _basePath;
private readonly string _tempDir;
private readonly Hashtable _files;
private bool _createdTempDirectory;
public int Count => _files.Count;
int ICollection.Count {
get {
return _files.Count;
}
}
object ICollection.SyncRoot {
get {
return null;
}
}
bool ICollection.IsSynchronized {
get {
return false;
}
}
public string TempDir => _tempDir ?? string.Empty;
public string BasePath {
get {
EnsureTempNameCreated();
return _basePath;
}
}
public bool KeepFiles { get; set; }
public TempFileCollection()
: this(null, false)
{
}
public TempFileCollection(string tempDir)
: this(tempDir, false)
{
}
public TempFileCollection(string tempDir, bool keepFiles)
{
KeepFiles = keepFiles;
_tempDir = tempDir;
_files = new Hashtable(StringComparer.OrdinalIgnoreCase);
}
void IDisposable.Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
internal void Dispose(bool disposing)
{
SafeDelete();
}
~TempFileCollection()
{
Dispose(false);
}
public string AddExtension(string fileExtension)
{
return AddExtension(fileExtension, KeepFiles);
}
public string AddExtension(string fileExtension, bool keepFile)
{
if (string.IsNullOrEmpty(fileExtension))
throw new ArgumentException(System.SR.Format(System.SR.InvalidNullEmptyArgument, "fileExtension"), "fileExtension");
string text = BasePath + "." + fileExtension;
AddFile(text, keepFile);
return text;
}
public void AddFile(string fileName, bool keepFile)
{
if (string.IsNullOrEmpty(fileName))
throw new ArgumentException(System.SR.Format(System.SR.InvalidNullEmptyArgument, "fileName"), "fileName");
if (_files[fileName] != null)
throw new ArgumentException(System.SR.Format(System.SR.DuplicateFileName, fileName), "fileName");
_files.Add(fileName, keepFile);
}
public IEnumerator GetEnumerator()
{
return _files.Keys.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return _files.Keys.GetEnumerator();
}
void ICollection.CopyTo(Array array, int start)
{
_files.Keys.CopyTo(array, start);
}
public void CopyTo(string[] fileNames, int start)
{
_files.Keys.CopyTo(fileNames, start);
}
private void EnsureTempNameCreated()
{
if (_basePath == null) {
int num = 5000;
string text;
bool flag;
do {
_basePath = Path.Combine(string.IsNullOrEmpty(TempDir) ? GetTempDirectory() : TempDir, Path.GetFileNameWithoutExtension(Path.GetRandomFileName()));
text = _basePath + ".tmp";
try {
new FileStream(text, FileMode.CreateNew, FileAccess.Write).Dispose();
flag = true;
} catch (IOException ex) {
num--;
if (num == 0 || ex is DirectoryNotFoundException)
throw;
flag = false;
}
} while (!flag);
_files.Add(text, KeepFiles);
}
}
private static string GetTempDirectory()
{
return Path.GetTempPath();
}
private bool KeepFile(string fileName)
{
object obj = _files[fileName];
if (obj == null)
return false;
return (bool)obj;
}
public void Delete()
{
SafeDelete();
}
private static void Delete(string fileName)
{
try {
File.Delete(fileName);
} catch {
}
}
internal void SafeDelete()
{
bool flag = true;
if (_files != null && _files.Count > 0) {
string[] array = new string[_files.Count];
_files.Keys.CopyTo(array, 0);
string[] array2 = array;
foreach (string text in array2) {
if (!KeepFile(text)) {
Delete(text);
_files.Remove(text);
} else
flag = false;
}
}
if (_createdTempDirectory & flag) {
try {
Directory.Delete(Path.GetDirectoryName(BasePath));
} catch {
}
_createdTempDirectory = false;
_basePath = null;
}
}
}
}