C# class that encapsulates the basic behaviors of temp files (and folders).
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
namespace TechTalk.SpecFlow.Utils
{
//
// this class is based on the class found at
// http://www.vcskicks.com/code-snippet/temp-file-class.php
//
public class TempFile : IDisposable
{
private readonly string _tmpfile;
public TempFile() : this(string.Empty) { }
public TempFile(string extension)
{
_tmpfile = Path.GetTempFileName();
if (!string.IsNullOrEmpty(extension))
{
string newTmpFile = _tmpfile + extension;
// create tmp-File with new extension ...
File.Create(newTmpFile).Dispose();
// delete old tmp-File
File.Delete(_tmpfile);
// use new tmp-File
_tmpfile = newTmpFile;
}
}
public void SetContent(string fileContent)
{
using(StreamWriter writer = new StreamWriter(FullPath, false, Encoding.UTF8))
{
writer.Write(fileContent);
}
}
public string FullPath
{
get
{
return _tmpfile;
}
}
public string FileName
{
get
{
return Path.GetFileName(FullPath);
}
}
public string FolderName
{
get
{
return Path.GetDirectoryName(FullPath);
}
}
void IDisposable.Dispose()
{
try
{
if (!string.IsNullOrEmpty(_tmpfile) && File.Exists(_tmpfile))
{
File.Delete(_tmpfile);
}
}
catch (Exception ex)
{
Debug.WriteLine(ex, "TempFile.Dispose");
}
}
}
public class TempFolder : IDisposable
{
private readonly string tempFolder;
public TempFolder()
{
tempFolder = Path.GetTempFileName();
// delete old tmp-File
File.Delete(tempFolder);
// create a temp folder
Directory.CreateDirectory(tempFolder);
}
public string FolderName
{
get
{
return tempFolder;
}
}
void IDisposable.Dispose()
{
try
{
if (!string.IsNullOrEmpty(tempFolder) && Directory.Exists(tempFolder))
{
Directory.Delete(tempFolder, true);
}
}
catch (Exception ex)
{
Debug.WriteLine(ex, "TempFolder.Dispose");
}
}
}
}
// Or... https://github.com/Azure/diagnostics-eventflow/blob/master/test/TestHelpers/TemporaryFile.cs
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License (MIT). See License.txt in the repo root for license information.
// ------------------------------------------------------------
using System;
using System.IO;
namespace Microsoft.Diagnostics.EventFlow.TestHelpers
{
public class TemporaryFile : IDisposable
{
public TemporaryFile() : this(Path.GetTempPath())
{ }
public TemporaryFile(string directory)
{
Create(Path.Combine(directory, Path.GetRandomFileName()));
}
public void Dispose()
{
Delete();
}
public void Write(string contents)
{
if (FilePath == null)
{
throw new ObjectDisposedException(nameof(TemporaryFile));
}
if (string.IsNullOrEmpty(contents))
{
return;
}
File.AppendAllText(FilePath, contents);
}
public void Clear()
{
if (FilePath == null)
{
throw new ObjectDisposedException(nameof(TemporaryFile));
}
File.WriteAllText(FilePath, string.Empty);
}
public string FilePath { get; private set; }
private void Create(string path)
{
FilePath = path;
using (File.Create(FilePath)) { };
}
private void Delete()
{
if (FilePath == null) return;
File.Delete(FilePath);
FilePath = null;
}
}
}