This class provides basic facilities for manipulating temporary files.
#region License and Copyright
/* -------------------------------------------------------------------------
* Dotnet Commons IO
*
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the
*
* Free Software Foundation, Inc.,
* 59 Temple Place,
* Suite 330,
* Boston,
* MA 02111-1307
* USA
*
* -------------------------------------------------------------------------
*/
#endregion
using System;
using System.IO;
namespace Dotnet.Commons.IO
{
/// <summary>
/// This class provides basic facilities for manipulating temporary files.
/// </summary>
/// <remarks>
/// This class includes code developed by
/// ThoughtWorks, Inc. (<a href="http://www.thoughtworks.com/">http://www.thoughtworks.com/</a>).
/// </remarks>
public sealed class TempFileUtils
{
private TempFileUtils() {}
/// <summary>
/// Create a temporary directory based on an object type
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static string CreateTempDir(object obj)
{
return CreateTempDir(obj.GetType().FullName);
}
/// <summary>
/// Create a temporary directory based on a given directory name
/// </summary>
/// <param name="dirname"></param>
/// <returns></returns>
public static string CreateTempDir(string dirname)
{
return CreateTempDir(dirname, true);
}
/// <summary>
/// Create a temporary directory given a directory name
/// </summary>
/// <param name="dirname"></param>
/// <param name="overwrite">overwrite the entire directory if set to true</param>
/// <returns></returns>
public static string CreateTempDir(string dirname, bool overwrite)
{
if (overwrite)
{
DeleteTempDir(dirname);
}
return Directory.CreateDirectory(GetTempPath(dirname)).FullName;
}
/// <summary>
/// Get the temporary directory name, which is a combine of the current
/// system temporary directory and a supplied directory name.
/// </summary>
/// <param name="dirname"></param>
/// <returns></returns>
public static string GetTempPath(string dirname)
{
return Path.Combine(Path.GetTempPath(), dirname);
}
/// <summary>
/// Get the temporary directory path, which is a combine of the current
/// system temporary directory (eg. C:\Temp)
/// and an object whose Type is to be used as a temporary folder name.
/// </summary>
/// <param name="obj">object to be used for creating a temporary folder name</param>
/// <returns></returns>
public static string GetTempPath(object obj)
{
return GetTempPath(obj.GetType().FullName);
}
/// <summary>
/// Get the full file path of a file in the temporary directory.
/// </summary>
/// <param name="dirname"></param>
/// <param name="filename"></param>
/// <returns></returns>
public static string GetTempFilePath(string dirname, string filename)
{
return Path.Combine(GetTempPath(dirname),filename);
}
/// <summary>
/// Clear the entire temporary directory and delete the directory when it is empty.
/// </summary>
/// <param name="dirname"></param>
/// <returns></returns>
public static bool DeleteTempDir(string dirname)
{
string tempDir = GetTempPath(dirname);
if (Directory.Exists(tempDir))
{
Directory.Delete(tempDir, true);
return true;
}
else
return false;
}
/// <summary>
/// Clear the entire temporary directory and delete the directory when it is empty.
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static bool DeleteTempDir(object obj)
{
return DeleteTempDir(obj.GetType().FullName);
}
/// <summary>
/// Check if a file exists in a temporary directory.
/// </summary>
/// <param name="dirname"></param>
/// <param name="filename"></param>
/// <returns></returns>
public static bool TempFileExists(string dirname, string filename)
{
return File.Exists(TempFileUtils.GetTempFilePath(dirname, filename));
}
/// <summary>
/// Create a file in a temporary directory
/// </summary>
/// <param name="tempDir"></param>
/// <param name="filename"></param>
/// <returns></returns>
public static string CreateTempTextFile(string tempDir, string filename)
{
string path = CreateTempDir(tempDir, false);
path = Path.Combine(path, filename);
using (File.CreateText(path))
{
return path;
}
}
/// <summary>
/// Create a text file in a temporary directory and text write the content to the file
/// </summary>
/// <param name="tempDir"></param>
/// <param name="filename"></param>
/// <param name="content"></param>
/// <returns></returns>
public static string CreateTempTextFile(string tempDir, string filename, string content)
{
string path = CreateTempDir(tempDir, false);
path = Path.Combine(path, filename);
using (StreamWriter stream = File.CreateText(path))
{
stream.Write(content);
}
return path;
}
/// <summary>
/// Create a binary file in a temporary directory and write the binary content to the file
/// </summary>
/// <param name="tempDir"></param>
/// <param name="filename"></param>
/// <param name="content">binary content as an array of bytes</param>
/// <returns></returns>
public static string CreateTempBinaryFile(string tempDir, string filename, byte[] content)
{
string path = CreateTempDir(tempDir, false);
path = Path.Combine(path, filename);
using (Stream fs = new FileStream(path, FileMode.Create))
{
fs.Write(content, 0, content.Length);
fs.Close();
}
return path;
}
/// <summary>
/// Update a temporary text file and append content to the end of the file.
/// </summary>
/// <param name="filename"></param>
/// <param name="content"></param>
public static void UpdateTempTextFile(string filename, string content)
{
using (StreamWriter writer = File.AppendText(filename))
{
writer.Write(content);
}
}
/// <summary>
/// Delete a file
/// </summary>
/// <param name="path"></param>
public static void DeleteTempFile(string path)
{
if (path != null && File.Exists(path))
{
File.Delete(path);
}
}
/// <summary>
/// Get the current System temporary directory
/// </summary>
/// <returns></returns>
public static string GetSystemTempDirectory()
{
return Path.GetTempPath();
}
/// <summary>
/// Get the directory setting from the Environment variable "TEMP"
/// </summary>
/// <returns></returns>
public static string GetEnvTempDirectory()
{
return Environment.GetEnvironmentVariable("TEMP");
}
}
}
Related examples in the same category