VirtualPathUtility Class
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Provides utility methods for common virtual path operations.
public ref class VirtualPathUtility abstract sealed
public static class VirtualPathUtility
type VirtualPathUtility = class
Public Class VirtualPathUtility
- Inheritance
-
VirtualPathUtility
The following code example demonstrates how to use the VirtualPathUtility class and some of its methods. First, the FilePath property generates the virtual path to the Web page. The GetFileName, GetExtension, and GetDirectory methods return information about the virtual path. Next, the CurrentExecutionFilePath property generates a virtual path of the current request, which might be different from the FilePath property, if a Redirect method was called. The IsAbsolute, IsAppRelative, and ToAppRelative methods return information about the virtual path.
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
// <Snippet2>
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());
// </Snippet2>
// <Snippet3>
StringBuilder sb2 = new StringBuilder();
String pathstring1 = Context.Request.CurrentExecutionFilePath.ToString();
sb2.Append("Current Executing File Path = " + pathstring1.ToString() + "<br />");
sb2.Append("Is Absolute = " + VirtualPathUtility.IsAbsolute(pathstring1).ToString() + "<br />");
sb2.Append("Is AppRelative = " + VirtualPathUtility.IsAppRelative(pathstring1).ToString() + "<br />");
sb2.Append("Make AppRelative = " + VirtualPathUtility.ToAppRelative(pathstring1).ToString() + "<br />");
Response.Write(sb2.ToString());
// </Snippet3>
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>VirtualPathUtility Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
' <Snippet2>
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())
' </Snippet2>
' <Snippet3>
Dim sb2 As New StringBuilder()
Dim pathstring1 As String = Context.Request.CurrentExecutionFilePath.ToString()
sb2.Append("Current Executing File Path = " & pathstring1.ToString() & "<br />")
sb2.Append("Is Absolute = " & VirtualPathUtility.IsAbsolute(pathstring1).ToString() & "<br />")
sb2.Append("Is AppRelative = " & VirtualPathUtility.IsAppRelative(pathstring1).ToString() & "<br />")
sb2.Append("Make AppRelative = " & VirtualPathUtility.ToAppRelative(pathstring1).ToString() & "<br />")
Response.Write(sb2.ToString())
' </Snippet3>
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>VirtualPathUtility Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
The VirtualPathUtility class provides utility methods for common operations involving virtual paths. For ASP.NET server controls and server code, virtual paths using the Web application root operator, the tilde (~), commonly are used instead of relative and absolute paths. For more information, see ASP.NET Web Project Paths.
Use the VirtualPathUtility class when you need to convert application-relative paths to absolute virtual paths, as might be the case in developing a custom Web service handler.
An absolute virtual path starts with the literal slash mark (/). A relative virtual path is relative to the application root directory, if it is just a tilde (~) or starts with the tilde and a double backslash (~\) or the tilde and a slash mark (~/). Making a virtual path relative makes the path independent of the application.
The virtual directory for the application can be obtained from the AppDomainAppVirtualPath and ApplicationPath properties.
Note
The VirtualPathUtility class is not intended for security or canonicalization purposes. For more information about Web application security, see Overview of Web Application Security Threats. For generic URL processing functionality, see Uri.
Append |
Appends the literal slash mark (/) to the end of the virtual path, if one does not already exist. |
Combine(String, String) |
Combines a base path and a relative path. |
Get |
Returns the directory portion of a virtual path. |
Get |
Retrieves the extension of the file that is referenced in the virtual path. |
Get |
Retrieves the file name of the file that is referenced in the virtual path. |
Is |
Returns a Boolean value indicating whether the specified virtual path is absolute; that is, it starts with a literal slash mark (/). |
Is |
Returns a Boolean value indicating whether the specified virtual path is relative to the application. |
Make |
Returns the relative virtual path from one virtual path containing the root operator (the tilde [~]) to another. |
Remove |
Removes a trailing slash mark (/) from a virtual path. |
To |
Converts a virtual path to an application absolute path using the specified application path. |
To |
Converts a virtual path to an application absolute path. |
To |
Converts a virtual path to an application-relative path using a specified application path. |
To |
Converts a virtual path to an application-relative path using the application virtual path that is in the AppDomainAppVirtualPath property. |
Product | Versions |
---|---|
.NET Framework | 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET feedback
.NET is an open source project. Select a link to provide feedback: