Assembly: mscorlib (in mscorlib.dll)
Public Shared Function HasExtension ( _ path As String _ ) As Boolean
Dim path As String Dim returnValue As Boolean returnValue = Path.HasExtension(path)
public static bool HasExtension ( string path )
public: static bool HasExtension ( String^ path )
public static boolean HasExtension ( String path )
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.| Exception type | Condition |
|---|---|
| path contains one or more of the invalid characters defined in InvalidPathChars, or contains a wildcard character. |
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. | |
| Write to a text file. | |
| Read from a text file. | |
| Retrieve a file extension. | |
| Retrieve the fully qualified path of a file. | |
| Retrieve the file name and extension from a path. | |
| Retrieve only the file name from a path. | |
| Retrieve only the directory name from a path. | |
| Change the extension of a file. | |
| Determine if a directory exists. | |
| Determine if a file exists. |
The following code example demonstrates the use of the HasExtension method.
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) ' This code produces output similar to the following: ' ' HasExtension('myfile.ext') returns True ' HasExtension('mydir\myfile') returns False ' HasExtension('C:\mydir.ext\') returns False
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); // This code produces output similar to the following: // // HasExtension('myfile.ext') returns True // HasExtension('mydir\myfile') returns False // HasExtension('C:\mydir.ext\') returns False
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.ToString() ); result = Path::HasExtension( fileName2 ); Console::WriteLine( "HasExtension('{0}') returns {1}", fileName2, result.ToString() ); result = Path::HasExtension( path ); Console::WriteLine( "HasExtension('{0}') returns {1}", path, result.ToString() );
String fileName1 = "myfile.ext";
String fileName2 = "mydir\\myfile";
String path = "C:\\mydir.ext\\";
boolean result;
result = Path.HasExtension(fileName1);
Console.WriteLine("HasExtension('{0}') returns {1}",
fileName1,System.Convert.ToString(result));
result = Path.HasExtension(fileName2);
Console.WriteLine("HasExtension('{0}') returns {1}",
fileName2, System.Convert.ToString(result));
result = Path.HasExtension(path);
Console.WriteLine("HasExtension('{0}') returns {1}",
path, System.Convert.ToString(result));
var fileName1 : String = "myfile.ext"; var fileName2 : String = "mydir\\myfile"; var path : String = "C:\\mydir.ext\\"; var result : 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(path); Console.WriteLine("HasExtension('{0}') returns {1}", path, result);
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.
.NET Framework
Supported in: 2.0, 1.1, 1.0.NET Compact Framework
Supported in: 2.0, 1.0Reference
Path ClassPath Members
System.IO Namespace
Other Resources
File and Stream I/OHow to: Read Text from a File
How to: Write Text to a File
