Click here to Skip to main content
15,893,487 members
Articles / Web Development / HTML

A WPF Template solution using MVVM and Castle Windsor

Rate me:
Please Sign up or sign in to vote.
4.78/5 (11 votes)
18 Nov 2013CPOL5 min read 34.9K   1.2K   19  
A WPF application base solution using Castle Windsor and commonly used utilities.
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Scodix
Israel Israel
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'.

Comments and Discussions