/// <summary>Determines whether a path includes a file name extension.</summary> /// <returns>true if the characters that follow the last directory separator (\\ or /) or volume separator (:) in the path include a period (.) followed by one or more characters; otherwise, false.</returns> /// <param name="path">The path to search for an extension. </param> /// <exception cref="T:System.ArgumentException"> /// <paramref name="path" /> contains one or more of the invalid characters defined in <see cref="M:System.IO.Path.GetInvalidPathChars" />. </exception> /// <filterpriority>1</filterpriority> public static bool HasExtension(string path) { if (path == null || path.Trim().Length == 0) { return(false); } if (path.IndexOfAny(Path.InvalidPathChars) != -1) { throw new ArgumentException("Illegal characters in path."); } int num = Path.findExtension(path); return(0 <= num && num < path.Length - 1); }
/// <summary>Returns the extension of the specified path string.</summary> /// <returns>A <see cref="T:System.String" /> containing the extension of the specified path (including the "."), null, or <see cref="F:System.String.Empty" />. If <paramref name="path" /> is null, GetExtension returns null. If <paramref name="path" /> does not have extension information, GetExtension returns Empty.</returns> /// <param name="path">The path string from which to get the extension. </param> /// <exception cref="T:System.ArgumentException"> /// <paramref name="path" /> contains one or more of the invalid characters defined in <see cref="M:System.IO.Path.GetInvalidPathChars" />. </exception> /// <filterpriority>1</filterpriority> public static string GetExtension(string path) { if (path == null) { return(null); } if (path.IndexOfAny(Path.InvalidPathChars) != -1) { throw new ArgumentException("Illegal characters in path."); } int num = Path.findExtension(path); if (num > -1 && num < path.Length - 1) { return(path.Substring(num)); } return(string.Empty); }
/// <summary>Changes the extension of a path string.</summary> /// <returns>A string containing the modified path information.On Windows-based desktop platforms, if <paramref name="path" /> is null or an empty string (""), the path information is returned unmodified. If <paramref name="extension" /> is null, the returned string contains the specified path with its extension removed. If <paramref name="path" /> has no extension, and <paramref name="extension" /> is not null, the returned path string contains <paramref name="extension" /> appended to the end of <paramref name="path" />.</returns> /// <param name="path">The path information to modify. The path cannot contain any of the characters defined in <see cref="M:System.IO.Path.GetInvalidPathChars" />. </param> /// <param name="extension">The new extension (with or without a leading period). Specify null to remove an existing extension from <paramref name="path" />. </param> /// <exception cref="T:System.ArgumentException"> /// <paramref name="path" /> contains one or more of the invalid characters defined in <see cref="M:System.IO.Path.GetInvalidPathChars" />.</exception> /// <filterpriority>1</filterpriority> public static string ChangeExtension(string path, string extension) { if (path == null) { return(null); } if (path.IndexOfAny(Path.InvalidPathChars) != -1) { throw new ArgumentException("Illegal characters in path."); } int num = Path.findExtension(path); if (extension == null) { return((num >= 0) ? path.Substring(0, num) : path); } if (extension.Length == 0) { return((num >= 0) ? path.Substring(0, num + 1) : (path + '.')); } if (path.Length != 0) { if (extension.Length > 0 && extension[0] != '.') { extension = "." + extension; } } else { extension = string.Empty; } if (num < 0) { return(path + extension); } if (num > 0) { string str = path.Substring(0, num); return(str + extension); } return(extension); }