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

System.IO.Path.GetExtension Method

Returns the extension component of the specified path string. [Edit]

public static string GetExtension (string path)

Parameters

path
A string containing the path information from which to get the extension. [Edit]

Returns

A string containing the extension of path,null , or string.Empty. If path is null, returns null. If path does not have extension information, returns string.Empty.

The extension returned by this method includes the implementation-specific extension separator character used to separate the extension from the rest of the path.

Platforms that do not support this feature return path unmodified.

[Edit]

Exceptions

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

Remarks

The exact behavior of this method is implementation-specific. The character used to separate the extension from the rest of the path is implementation-specific. [Edit]

Example

The following example demonstrates using the Path.GetExtension(string) method on a Windows system.

C# Example
using System;
using System.IO;
class GetDirectoryTest {
 public static void Main(){
   string [] paths = {
     @"\ecmatest\examples\pathtests.txt",
     @"\ecmatest\examples\",
     "pathtests.xyzzy",
     "pathtests.xyzzy.txt",
     @"\",
     ""
   };
   foreach (string pathString in paths){
     string s = Path.GetExtension (pathString);
     if (s == String.Empty) s= "(empty string)";
     if (s == null) s= "null";
     Console.WriteLine("{0} is the extension of {1}", s, pathString);
   }
 }
}

The output is

.txt is the extension of \ecmatest\examples\pathtests.txt
(empty string) is the extension of \ecmatest\examples\
.xyzzy is the extension of pathtests.xyzzy
.txt is the extension of pathtests.xyzzy.txt
(empty string) is the extension of \
(empty string) is the extension of

Requirements

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