This class provides basic facilities for manipulating temporary files. : Temp File « File Stream « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# Book
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » File Stream » Temp FileScreenshots 
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
ww_w_.j_a___va__2__s__.___c___om__ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.