Mono Class Library: System.IO.Path Overview | Members

System.IO.Path.GetFileName Method

Returns the file name, including the extension if any, of the specified path string.

public static string GetFileName (string path)

Parameters

path
A string containing the path information from which to obtain the filename and extension.

Returns

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.

Exceptions

TypeReason
ArgumentExceptionpath contains one or more implementation-specific invalid characters.

Remarks

The directory separator characters used to determine the start of the file name are Path.DirectorySeparatorChar and Path.AltDirectorySeparatorChar.

Example

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 =

Requirements

Namespace: System.IO
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0