#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
{
///
/// This class provides basic facilities for manipulating temporary files.
///
///
/// This class includes code developed by
/// ThoughtWorks, Inc. (http://www.thoughtworks.com/).
///
public sealed class TempFileUtils
{
private TempFileUtils() {}
///
/// Create a temporary directory based on an object type
///
///
///
public static string CreateTempDir(object obj)
{
return CreateTempDir(obj.GetType().FullName);
}
///
/// Create a temporary directory based on a given directory name
///
///
///
public static string CreateTempDir(string dirname)
{
return CreateTempDir(dirname, true);
}
///
/// Create a temporary directory given a directory name
///
///
/// overwrite the entire directory if set to true
///
public static string CreateTempDir(string dirname, bool overwrite)
{
if (overwrite)
{
DeleteTempDir(dirname);
}
return Directory.CreateDirectory(GetTempPath(dirname)).FullName;
}
///
/// Get the temporary directory name, which is a combine of the current
/// system temporary directory and a supplied directory name.
///
///
///
public static string GetTempPath(string dirname)
{
return Path.Combine(Path.GetTempPath(), dirname);
}
///
/// 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.
///
/// object to be used for creating a temporary folder name
///
public static string GetTempPath(object obj)
{
return GetTempPath(obj.GetType().FullName);
}
///
/// Get the full file path of a file in the temporary directory.
///
///
///
///
public static string GetTempFilePath(string dirname, string filename)
{
return Path.Combine(GetTempPath(dirname),filename);
}
///
/// Clear the entire temporary directory and delete the directory when it is empty.
///
///
///
public static bool DeleteTempDir(string dirname)
{
string tempDir = GetTempPath(dirname);
if (Directory.Exists(tempDir))
{
Directory.Delete(tempDir, true);
return true;
}
else
return false;
}
///
/// Clear the entire temporary directory and delete the directory when it is empty.
///
///
///
public static bool DeleteTempDir(object obj)
{
return DeleteTempDir(obj.GetType().FullName);
}
///
/// Check if a file exists in a temporary directory.
///
///
///
///
public static bool TempFileExists(string dirname, string filename)
{
return File.Exists(TempFileUtils.GetTempFilePath(dirname, filename));
}
///
/// Create a file in a temporary directory
///
///
///
///
public static string CreateTempTextFile(string tempDir, string filename)
{
string path = CreateTempDir(tempDir, false);
path = Path.Combine(path, filename);
using (File.CreateText(path))
{
return path;
}
}
///
/// Create a text file in a temporary directory and text write the content to the file
///
///
///
///
///
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;
}
///
/// Create a binary file in a temporary directory and write the binary content to the file
///
///
///
/// binary content as an array of bytes
///
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;
}
///
/// Update a temporary text file and append content to the end of the file.
///
///
///
public static void UpdateTempTextFile(string filename, string content)
{
using (StreamWriter writer = File.AppendText(filename))
{
writer.Write(content);
}
}
///
/// Delete a file
///
///
public static void DeleteTempFile(string path)
{
if (path != null && File.Exists(path))
{
File.Delete(path);
}
}
///
/// Get the current System temporary directory
///
///
public static string GetSystemTempDirectory()
{
return Path.GetTempPath();
}
///
/// Get the directory setting from the Environment variable "TEMP"
///
///
public static string GetEnvTempDirectory()
{
return Environment.GetEnvironmentVariable("TEMP");
}
}
}