using System;
using System.IO;
namespace Core.Common.TestUtil
{
///
/// This class can be used to set a temporary file while testing.
/// Disposing an instance of this class will delete the file.
///
///
/// The following is an example for how to use this class:
///
/// using(new FileDisposeHelper("pathToFile")) {
/// // Perform tests with file
/// }
///
///
public class FileDisposeHelper : IDisposable
{
private readonly string filePath;
///
/// Creates a new instance of .
///
/// Path of the file that will be used.
public FileDisposeHelper(string filePath)
{
this.filePath = filePath;
}
///
/// Creates the temporary file.
///
public void CreateFile()
{
using (File.Create(filePath)) {}
}
///
/// Disposes the instance.
///
public void Dispose()
{
GC.Collect();
GC.WaitForPendingFinalizers();
if (!string.IsNullOrWhiteSpace(filePath))
{
File.Delete(filePath);
}
}
}
}