External File Dependency in NUnit
Sunday, February 19 2006 - Blog
Fortunately for me I remember reading about a way to handle this some time ago on Scott Hanselman's blog.He was in fact just quoting Patrick Cauldwell.Both of the variations of handling this were good, but not perfectly portable.I realized that this could be encapsulated into a re-usable component which implemented the IDisposable interface.This would allow me to use the using() statement and ensure that files were always cleaned up rather than accidently forgotten when you called only half of the routines presented by Scott or Patrick.So without further ado, here is my TestFile class.
public class TestFile : IDisposable
{
private bool _disposedValue = false;
private string _resourceName;
private string _fileName;
public TestFile(string fileName, string resourceName)
{
_resourceName = resourceName;
_fileName = fileName;
Assembly a = Assembly.GetExecutingAssembly();
using (Stream s = a.GetManifestResourceStream(_resourceName))
{
if (s == null) throw new Exception("Manifest Resource Stream " + _resourceName + " was not found.");
using (StreamReader sr = new StreamReader(s))
{
using (StreamWriter sw = File.CreateText(_fileName))
{
sw.Write(sr.ReadToEnd());
sw.Flush();
}
}
}
}
protected virtual void Dispose(bool disposing)
{
if (!this._disposedValue)
{
if (disposing)
{
if (File.Exists(_fileName))
{
File.Delete(_fileName);
}
}
}
this._disposedValue = true;
}
#region IDisposable Members
public void Dispose()
{
// Do not change this code.Put cleanup code in Dispose(bool disposing) above.
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
}



