/// <summary> /// Converts a long filename to a short 8.3 filename. /// </summary> /// <param name="fileName">The long filename.</param> /// <returns>The converted 8.3 filename. If the filename includes a /// starting path then this is stripped from the returned value.</returns> public static string Convert(string fileName, NameExists getFileNameExists) { if (fileName == null) { return String.Empty; } // Remove invalid characters. string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileName); string extension = Path.GetExtension(fileName); StringBuilder shortFileName = new StringBuilder(); int length = 0; foreach (char ch in fileNameWithoutExtension) { if (IsValidShortFileNameChar(ch)) { shortFileName.Append(ch); ++length; } } // Get truncated extension. if (extension.Length > MaximumFileNameExtensionLength) { extension = extension.Substring(0, MaximumFileNameExtensionLength); } // Truncate filename without extension if it is too long. if (length > MaximumFileNameWithoutExtensionLength) { shortFileName.Remove(FileNameWithoutExtensionTruncatedLength, length - FileNameWithoutExtensionTruncatedLength); return GetTruncatedFileName(shortFileName.ToString(), extension, getFileNameExists); } // Add extension. shortFileName.Append(extension); return shortFileName.ToString().ToUpperInvariant(); }
/// <summary> /// Converts a long filename to a short 8.3 filename. /// </summary> /// <param name="fileName">The long filename.</param> /// <returns>The converted 8.3 filename. If the filename includes a /// starting path then this is stripped from the returned value.</returns> public static string Convert(string fileName, NameExists getFileNameExists) { if (fileName == null) { return(String.Empty); } // Remove invalid characters. string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileName); string extension = Path.GetExtension(fileName); StringBuilder shortFileName = new StringBuilder(); int length = 0; foreach (char ch in fileNameWithoutExtension) { if (IsValidShortFileNameChar(ch)) { shortFileName.Append(ch); ++length; } } // Get truncated extension. if (extension.Length > MaximumFileNameExtensionLength) { extension = extension.Substring(0, MaximumFileNameExtensionLength); } // Truncate filename without extension if it is too long. if (length > MaximumFileNameWithoutExtensionLength) { shortFileName.Remove(FileNameWithoutExtensionTruncatedLength, length - FileNameWithoutExtensionTruncatedLength); return(GetTruncatedFileName(shortFileName.ToString(), extension, getFileNameExists)); } // Add extension. shortFileName.Append(extension); return(shortFileName.ToString().ToUpperInvariant()); }
/// <summary> /// Truncates the filename start and adds "_N" where N produces a filename that /// does not exist. /// </summary> static string GetTruncatedFileName(string fileNameStart, string extension, NameExists getFileNameExists) { return GetUniqueName(fileNameStart.ToUpperInvariant(), "_", extension.ToUpperInvariant(), getFileNameExists); }
/// <summary> /// Gets the unique name string by adding a digit until a unique name is /// produced. The name string itself will be truncated as the number /// increases in size to make sure the total length of the string is /// always equal to start.Length + numberPrefix.Length + 1 + end.Length. /// </summary> /// <param name="start">The string to be used at the start of the </param> /// <param name="numberPrefix">The string to be prefixed to the number.</param> /// <param name="end">The string to be added after the number.</param> /// <param name="getNameExists">Method called to check that the /// name does not already exist.</param> public static string GetUniqueName(string start, string numberPrefix, string end, NameExists getNameExists) { int count = 0; string truncatedName; int divisor = 10; do { ++count; int removeCharCount = count / divisor; if (removeCharCount > 0) { start = start.Substring(0, start.Length - 1); divisor *= divisor; } truncatedName = String.Concat(start, numberPrefix, count.ToString(), end); } while (getNameExists(truncatedName)); return truncatedName; }
/// <summary> /// Gets the unique name string by adding a digit until a unique name is /// produced. The name string itself will be truncated as the number /// increases in size to make sure the total length of the string is /// always equal to start.Length + 1. /// </summary> /// <param name="start">The string to be used at the start of the </param> /// <param name="getNameExists">Method called to check that the /// name does not already exist.</param> public static string GetUniqueName(string start, NameExists getNameExists) { return GetUniqueName(start, String.Empty, String.Empty, getNameExists); }
/// <summary> /// Truncates the filename start and adds "_N" where N produces a filename that /// does not exist. /// </summary> static string GetTruncatedFileName(string fileNameStart, string extension, NameExists getFileNameExists) { return(GetUniqueName(fileNameStart.ToUpperInvariant(), "_", extension.ToUpperInvariant(), getFileNameExists)); }
/// <summary> /// Gets the unique name string by adding a digit until a unique name is /// produced. The name string itself will be truncated as the number /// increases in size to make sure the total length of the string is /// always equal to start.Length + numberPrefix.Length + 1 + end.Length. /// </summary> /// <param name="start">The string to be used at the start of the </param> /// <param name="numberPrefix">The string to be prefixed to the number.</param> /// <param name="end">The string to be added after the number.</param> /// <param name="getNameExists">Method called to check that the /// name does not already exist.</param> public static string GetUniqueName(string start, string numberPrefix, string end, NameExists getNameExists) { int count = 0; string truncatedName; int divisor = 10; do { ++count; int removeCharCount = count / divisor; if (removeCharCount > 0) { start = start.Substring(0, start.Length - 1); divisor *= divisor; } truncatedName = String.Concat(start, numberPrefix, count.ToString(), end); } while (getNameExists(truncatedName)); return(truncatedName); }
/// <summary> /// Gets the unique name string by adding a digit until a unique name is /// produced. The name string itself will be truncated as the number /// increases in size to make sure the total length of the string is /// always equal to start.Length + 1. /// </summary> /// <param name="start">The string to be used at the start of the </param> /// <param name="getNameExists">Method called to check that the /// name does not already exist.</param> public static string GetUniqueName(string start, NameExists getNameExists) { return(GetUniqueName(start, String.Empty, String.Empty, getNameExists)); }