VirtualPathUtility.GetExtension(String) 方法

定义

检索虚拟路径中引用的文件的扩展名。

public:
 static System::String ^ GetExtension(System::String ^ virtualPath);
public static string GetExtension (string virtualPath);
static member GetExtension : string -> string
Public Shared Function GetExtension (virtualPath As String) As String

参数

virtualPath
String

虚拟路径。

返回

文件扩展名字符串,包括句点 (.)、null 或空字符串 ("")。

例外

virtualPath 包含 InvalidPathChars 中定义的一个或多个无效字符。

示例

下面的代码示例演示如何使用 GetFileNameGetExtensionGetDirectory 方法。

StringBuilder sb = new StringBuilder();
String pathstring = Context.Request.FilePath.ToString();
sb.Append("Current file path = " + pathstring + "<br />");
sb.Append("File name = " + VirtualPathUtility.GetFileName(pathstring).ToString() + "<br />");
sb.Append("File extension = " + VirtualPathUtility.GetExtension(pathstring).ToString() + "<br />");
sb.Append("Directory = " + VirtualPathUtility.GetDirectory(pathstring).ToString() + "<br />");
Response.Write(sb.ToString());
Dim sb As New StringBuilder()
Dim pathstring As String = Context.Request.FilePath.ToString()
sb.Append("Current file path = " & pathstring & "<br />")
sb.Append("File name = " & VirtualPathUtility.GetFileName(pathstring).ToString() & "<br />")
sb.Append("File extension = " & VirtualPathUtility.GetExtension(pathstring).ToString() & "<br />")
sb.Append("Directory = " & VirtualPathUtility.GetDirectory(pathstring).ToString() & "<br />")
Response.Write(sb.ToString())

注解

如果传入方法的 GetExtension 虚拟路径为 "/images/image1.gif",则返回的扩展名为 ".gif"

适用于

另请参阅