/// <summary> /// Replaces invalid file name chars in the specified string and changes it if it is a reserved file name. /// </summary> /// <param name="fileName">the name of the file</param> /// <returns>Replaced string.</returns> public static string ReplaceInvalidFileNameChars(string fileName) { EqtAssert.StringNotNullOrEmpty(fileName, "fileName"); // Replace bad chars by this. char replacementChar = '_'; StringBuilder result = new StringBuilder(fileName.Length); result.Length = fileName.Length; // Replace each invalid char with replacement char. for (int i = 0; i < fileName.Length; ++i) { if (InvalidFileNameChars.ContainsKey(fileName[i]) || AdditionalInvalidFileNameChars.ContainsKey(fileName[i])) { result[i] = replacementChar; } else { result[i] = fileName[i]; } } // We trim spaces in the end because CreateFile/Dir trim those. string replaced = result.ToString().TrimEnd(); if (replaced.Length == 0) { Debug.Fail(string.Format(CultureInfo.InvariantCulture, "After replacing invalid chars in file '{0}' there's nothing left...", fileName)); throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, TrxLoggerResources.Common_NothingLeftAfterReplaciingBadCharsInName, fileName)); } if (IsReservedFileName(replaced)) { replaced = replacementChar + replaced; // Cannot add to the end because it can have extensions. } return(replaced); }
public static string MakePathRelative(string path, string basePath) { EqtAssert.StringNotNullOrEmpty(path, "path"); // Can't be relative to nothing if (string.IsNullOrEmpty(basePath)) { return(path); } // Canonicalize those paths: if (!Path.IsPathRooted(path)) { //If path is relative, we combine it with base path before canonicalizing. //Else Path.GetFullPath is going to use the process worker directory (e.g. e:\binariesy.x86\bin\i386). path = Path.Combine(basePath, path); } path = Path.GetFullPath(path); basePath = Path.GetFullPath(basePath); char[] delimiters = new char[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar }; basePath = basePath.TrimEnd(delimiters); path = path.TrimEnd(delimiters); string[] pathTokens = path.Split(delimiters); string[] basePathTokens = basePath.Split(delimiters); Debug.Assert(pathTokens.Length > 0 && basePathTokens.Length > 0); int max = Math.Min(pathTokens.Length, basePathTokens.Length); // Skip all of the empty tokens that result from things like "\dir1" // and "\\dir1". We need to compare the first non-null token // to know if we've got differences. int i = 0; for (i = 0; i < max && pathTokens[i].Length == 0 && basePathTokens[i].Length == 0; i++) { ; } if (i >= max) { // At least one of these strings is too short to work with return(path); } if (!pathTokens[i].Equals(basePathTokens[i], StringComparison.OrdinalIgnoreCase)) { // These differ from the very start - just return the original path return(path); } for (++i; i < max; i++) { if (!pathTokens[i].Equals(basePathTokens[i], StringComparison.OrdinalIgnoreCase)) { // We've found a non-matching token break; } } // i should point to first non-matching token. StringBuilder newPath = new StringBuilder(); // ok, for each remaining token in the base path, // add ..\ to the string. for (int j = i; j < basePathTokens.Length; j++) { if (newPath.Length > 0) { newPath.Append(Path.DirectorySeparatorChar); } newPath.Append(RelativeDirectorySeparator); } // And now, for every remaining token in the path, // add it to the string, separated by the directory // separator. for (int j = i; j < pathTokens.Length; j++) { if (newPath.Length > 0) { newPath.Append(Path.DirectorySeparatorChar); } newPath.Append(pathTokens[j]); } return(newPath.ToString()); }
/// <summary> /// Checks whether file with specified name exists in the specified directory. /// If it exits, adds (1),(2)... to the file name and checks again. /// Returns full file name (with path) of the iteration when the file does not exist. /// </summary> /// <param name="parentDirectoryName"> /// The directory where to check. /// </param> /// <param name="originalFileName"> /// The original file (that we would add (1),(2),.. in the end of if needed) name to check. /// </param> /// <param name="checkMatchingDirectory"> /// If true, and directory with filename without extension exists, try next iteration. /// </param> /// <returns> /// The <see cref="string"/>. /// </returns> public static string GetNextIterationFileName(string parentDirectoryName, string originalFileName, bool checkMatchingDirectory) { EqtAssert.StringNotNullOrEmpty(parentDirectoryName, "parentDirectoryName"); EqtAssert.StringNotNullOrEmpty(originalFileName, "originalFileName"); return(GetNextIterationNameHelper(parentDirectoryName, originalFileName, new FileIterationHelper(checkMatchingDirectory))); }