Returns the file name, including the extension if any, of the specified path string.
- path
- A string containing the path information from which to obtain the filename and extension.
A string consisting of the characters after the last directory character in path. If the last character of path is a directory separator character, returns string.Empty. If path is null, returns null.
Platforms that do not support this feature return path unmodified.
Type Reason ArgumentException path contains one or more implementation-specific invalid characters.
The directory separator characters used to determine the start of the file name are Path.DirectorySeparatorChar and Path.AltDirectorySeparatorChar.
The following example demonstrates the behavior of the Path.GetFileName(string) method on a Windows system.
C# Example
using System; using System.IO; class FileNameTest { public static void Main() { string [] paths = {"pathtests.txt", @"\ecmatest\examples\pathtests.txt", "c:pathtests.txt", @"\ecmatest\examples\", "" }; foreach (string p in paths) { Console.WriteLine("Path: {0} filename = {1}",p, Path.GetFileName(p)); } } }The output is
Path: pathtests.txt filename = pathtests.txt
Path: \ecmatest\examples\pathtests.txt filename = pathtests.txt
Path: c:pathtests.txt filename = pathtests.txt
Path: \ecmatest\examples\ filename =
Path: filename =
Namespace: System.IO
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0