Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Path.HasExtension Method

Determines whether a path includes a file name extension.

[Visual Basic]
Public Shared Function HasExtension( _
   ByVal path As String _
) As Boolean
[C#]
public static bool HasExtension(
 string path
);
[C++]
public: static bool HasExtension(
 String* path
);
[JScript]
public static function HasExtension(
   path : String
) : Boolean;

Parameters

path
The path to search for an extension.

Return Value

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.

Exceptions

Exception Type Condition
ArgumentException path contains one or more of the invalid characters defined in InvalidPathChars.

Remarks

Starting from the end of path, this method searches for a period (.) followed by at least one character. If this pattern is found before a DirectorySeparatorChar, AltDirectorySeparatorChar, or VolumeSeparatorChar character is encountered, this method returns true.

For an example of using this method, see the Example section below. The following table lists examples of other typical or related I/O tasks.

To do this... See the example in this topic...
Create a text file. Writing Text to a File
Write to a text file. Writing Text to a File
Read from a text file. Reading Text from a File
Retrieve a file extension. GetExtension
Retrieve the fully qualified path of a file. GetFullPath
Retrieve the file name and extension from a path. GetFileName
Retrieve only the file name from a path. GetFileNameWithoutExtension
Retrieve only the directory name from a path. GetDirectoryName
Change the extension of a file. ChangeExtension
Determine if a directory exists. Exists
Determine if a file exists. Exists

Example

[Visual Basic, C#, C++] The following example demonstrates the use of the HasExtension method.

[Visual Basic] 
Dim fileName1 As String = "myfile.ext"
Dim fileName2 As String = "mydir\myfile"
Dim pathname As String = "C:\mydir.ext\"
Dim result As Boolean

result = Path.HasExtension(fileName1)
Console.WriteLine("HasExtension('{0}') returns {1}", fileName1, result)

result = Path.HasExtension(fileName2)
Console.WriteLine("HasExtension('{0}') returns {1}", fileName2, result)

result = Path.HasExtension(pathname)
Console.WriteLine("HasExtension('{0}') returns {1}", pathname, result)

[C#] 
string fileName1 = "myfile.ext";
string fileName2 = @"mydir\myfile";
string path = @"C:\mydir.ext\";
bool result;

result = Path.HasExtension(fileName1);
Console.WriteLine("HasExtension('{0}') returns {1}", 
    fileName1, result);

result = Path.HasExtension(fileName2);
Console.WriteLine("HasExtension('{0}') returns {1}", 
    fileName2, result);

result = Path.HasExtension(path);
Console.WriteLine("HasExtension('{0}') returns {1}", 
    path, result);

[C++] 
String * fileName1 = S"myfile.ext";
String * fileName2 = S"mydir\\myfile";
String * path = S"C:\\mydir.ext\\";
bool result;

result = Path::HasExtension(fileName1);
Console::WriteLine(S"HasExtension('{0}') returns {1}", 
   fileName1, result.ToString());

result = Path::HasExtension(fileName2);
Console::WriteLine(S"HasExtension('{0}') returns {1}", 
   fileName2, result.ToString());

result = Path::HasExtension(path);
Console::WriteLine(S"HasExtension('{0}') returns {1}", 
   path, result.ToString());

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

Path Class | Path Members | System.IO Namespace | Working with I/O | Reading Text from a File | Writing Text to a File

Show:
© 2014 Microsoft