|
using System;
using System.IO;
namespace CommonUtilities
{
public class FileHelper
{
/// <summary>
/// rename file to tmp name - the method also makes sure that the result file length is not exceeding the
/// allowed file name length in NTFS
/// In case the file name exceed it is trimmed!
/// </summary>
/// <param name="fileName">file name</param>
/// <returns>tmp file name</returns>
public static string Rename2Tmp(ref string fileName)
{
string originalName = fileName;
string tmpFileName = TmpFileName(ref fileName);
File.Move(originalName, tmpFileName);
return tmpFileName;
}
private const int TEMP_FILE_BUFFER = 45;
private const int MAX_FILE_LENGH = 260;
/// <summary>
/// Temporaries the name of the file.
/// </summary>
/// <param name="fileName">Name of the file.</param>
/// <returns></returns>
public static string TmpFileName(string fileName)
{
return TmpFileName(ref fileName);
}
/// <summary>
/// Generate tmp file name - the method also makes sure that the result file length is not exceeding the
/// allowed file name length in NTFS
/// In case the file name exceed it is trimmed!
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
public static string TmpFileName(ref string fileName)
{
string dirName = Path.GetDirectoryName(fileName) ?? string.Empty;
string extName = Path.GetExtension(fileName);
string fileNoExt = Path.GetFileNameWithoutExtension(fileName);
if (fileName.Length >= MAX_FILE_LENGH - TEMP_FILE_BUFFER) // taking 60 chars as buffer
{
int trimSize = Math.Min(TEMP_FILE_BUFFER, fileName.Length - (MAX_FILE_LENGH - TEMP_FILE_BUFFER));
if (fileNoExt.Length > trimSize)
{
fileNoExt = fileNoExt.Substring(0, fileNoExt.Length - trimSize);
fileName = Path.Combine(dirName, fileNoExt + extName);
}
}
string tmpFileName = string.Format(@"{0}{1}{2}{3}{4}{5}{6}{7}",
dirName, (dirName.Length > 0 ? @"\" : ""),
fileNoExt,
"{", System.Guid.NewGuid(), "}",
".tmp",
!string.IsNullOrEmpty(extName) ? Path.GetExtension(fileName).Replace(".", "~"): "~");
//
return tmpFileName;
}
/// <summary>
/// original file name from tmp file name
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
public static string Tmp2OriginalName(string fileName)
{
string extName = Path.GetExtension(fileName);
if (extName.Length > 4)
{
// validate
if (extName.Substring(0, 5) != ".tmp~") //when not a temporary name
{
return fileName;
}
extName = extName.Substring(4).Replace("~", ".");
}
string dirName = Path.GetDirectoryName(fileName);
string bodyName = Path.GetFileNameWithoutExtension(fileName);
if (bodyName.Length > 38) //when contains Guid
{
bodyName = bodyName.Substring(0, bodyName.Length - 38);
}
string orgFileName = string.Format(@"{0}{1}{2}{3}",
dirName,
(dirName.Length > 0 ? @"\" : ""),
bodyName,
extName != "." ?
extName : string.Empty);
//
return orgFileName;
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Project Manager and Application Developer, in a wide variety of business applications. Particularly interested in client/server and Graphical User Interface design using Visual C#.
Specialties: 13 Y'rs in C#, 10 Y'rs experience in C++ Highly experienced in wide technologies, IT projects, military projects etc'.