ASP.NET Tutorial/File Directory/Path
Содержание
File Path Parsing (C#)
<%@ Page Language="C#" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.IO" %>
<script runat="server">
void Page_Load(Object Source, EventArgs E)
{
PathPropertiesLiteral.Text = "";
}
void ParsePath(Object Source, EventArgs E)
{
StringBuilder PropertyContainer = new StringBuilder();
PropertyContainer.Append("<Table border="0" width="100%">");
PropertyContainer.Append("<Tr><Td>Full Path</Td>");
PropertyContainer.Append("<Td>" + Path.GetFullPath(FileNameTextBox.Text) + "</Td></Tr>");
PropertyContainer.Append("<Tr><Td>Root Drive</Td>");
PropertyContainer.Append("<Td>" + Path.GetPathRoot(FileNameTextBox.Text) + "</Td></Tr>");
PropertyContainer.Append("<Tr><Td>Directory Path</Td>");
PropertyContainer.Append("<Td>" + Path.GetDirectoryName(FileNameTextBox.Text) + "</Td></Tr>");
PropertyContainer.Append("<Tr><Td>File Name</Td>");
PropertyContainer.Append("<Td>" + Path.GetFileName(FileNameTextBox.Text) + "</Td></Tr>");
PropertyContainer.Append("<Tr><Td>File Name (Without Extension)</Td>");
PropertyContainer.Append("<Td>" + Path.GetFileNameWithoutExtension(FileNameTextBox.Text) + "</Td></Tr>");
PropertyContainer.Append("<Tr><Td>File Extension</Td>");
PropertyContainer.Append("<Td>" + Path.GetExtension(FileNameTextBox.Text) + "</Td></Tr>");
PropertyContainer.Append("<Tr><Td>Temporary Filename</Td>");
PropertyContainer.Append("<Td>" + Path.GetTempFileName() + "</Td></Tr>");
PropertyContainer.Append("<Tr><Td>Temprary Filepath</Td>");
PropertyContainer.Append("<Td>" + Path.GetTempPath() + "</Td></Tr>");
PropertyContainer.Append("<Tr><Td>Directory Separator</Td>");
PropertyContainer.Append("<Td>" + Path.DirectorySeparatorChar + "</Td></Tr>");
PropertyContainer.Append("<Tr><Td>Alt Directory Separator</Td>");
PropertyContainer.Append("<Td>" + Path.AltDirectorySeparatorChar + "</Td></Tr>");
PropertyContainer.Append("<Tr><Td>Path Separator</Td>");
PropertyContainer.Append("<Td>" + Path.PathSeparator + "</Td></Tr>");
PropertyContainer.Append("<Tr><Td>Volume Separator</Td>");
PropertyContainer.Append("<Td>" + Path.VolumeSeparatorChar + "</Td></Tr>");
PropertyContainer.Append("<Tr><Td>Invalid Path Characters</Td>");
PropertyContainer.Append("<Td>" + Path.InvalidPathChars + "</Td></Tr>");
PropertyContainer.Append("</Table>");
PathPropertiesLiteral.Text = PropertyContainer.ToString();
}
</script>
<html>
<head>
<title>File Path Parsing</title>
</head>
<body>
<form runat="server">
<h4>File Path Parser
</h4>
<h4>
<asp:TextBox id="FileNameTextBox" runat="server"></asp:TextBox>
<asp:Button id="ParsePathButton" onclick="ParsePath" runat="server" Text="Parse"></asp:Button>
</h4>
<hr />
</form>
<asp:Literal id="PathPropertiesLiteral" runat="server"></asp:Literal>
</body>
</html>
File Path Parsing (VB)
<%@ Page Language="VB" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.IO" %>
<script runat="server">
Public Sub Page_Load(Source As Object, E As EventArgs)
PathPropertiesLiteral.Text = ""
End Sub
Public Sub ParsePath(Source As Object, E As EventArgs)
Dim PropertyContainer As New StringBuilder()
With PropertyContainer
.Append("<Table border="0" width="100%">")
.Append("<Tr><Td>Full Path</Td>")
.Append("<Td>" & Path.GetFullPath(FileNameTextBox.Text) & "</Td></Tr>")
.Append("<Tr><Td>Root Drive</Td>")
.Append("<Td>" & Path.GetPathRoot(FileNameTextBox.Text) & "</Td></Tr>")
.Append("<Tr><Td>Directory Path</Td>")
.Append("<Td>" & Path.GetDirectoryName(FileNameTextBox.Text) & "</Td></Tr>")
.Append("<Tr><Td>File Name</Td>")
.Append("<Td>" & Path.GetFileName(FileNameTextBox.Text) & "</Td></Tr>")
.Append("<Tr><Td>File Name (Without Extension)</Td>")
.Append("<Td>" & Path.GetFileNameWithoutExtension(FileNameTextBox.Text) & "</Td></Tr>")
.Append("<Tr><Td>File Extension</Td>")
.Append("<Td>" & Path.GetExtension(FileNameTextBox.Text) & "</Td></Tr>")
.Append("<Tr><Td>Temporary Filename</Td>")
.Append("<Td>" & Path.GetTempFileName() & "</Td></Tr>")
.Append("<Tr><Td>Temprary Filepath</Td>")
.Append("<Td>" & Path.GetTempPath() & "</Td></Tr>")
.Append("<Tr><Td>Directory Separator</Td>")
.Append("<Td>" & Path.DirectorySeparatorChar & "</Td></Tr>")
.Append("<Tr><Td>Alt Directory Separator</Td>")
.Append("<Td>" & Path.AltDirectorySeparatorChar & "</Td></Tr>")
.Append("<Tr><Td>Path Separator</Td>")
.Append("<Td>" & Path.PathSeparator & "</Td></Tr>")
.Append("<Tr><Td>Volume Separator</Td>")
.Append("<Td>" & Path.VolumeSeparatorChar & "</Td></Tr>")
.Append("<Tr><Td>Invalid Path Characters</Td>")
.Append("<Td>" & Path.InvalidPathChars & "</Td></Tr>")
.Append("</Table>")
End With
PathPropertiesLiteral.Text = PropertyContainer.ToString
End Sub
</script>
<html>
<head>
<title>CookBook :: File Path Parsing</title>
</head>
<body>
<form runat="server">
<h4>File Path Parser
</h4>
<h4>
<asp:TextBox id="FileNameTextBox" runat="server"></asp:TextBox>
<asp:Button id="ParsePathButton" onclick="ParsePath" runat="server" Text="Parse"></asp:Button>
</h4>
<hr />
</form>
<asp:Literal id="PathPropertiesLiteral" runat="server"></asp:Literal>
</body>
</html>
Using the Path class (C#)
<%@ Page Language="C#" %>
<%@ Import Namespace="System.IO" %>
<!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)
{
if (Page.IsPostBack)
{
this.lblRootPath.Text = Path.GetPathRoot(this.txtPathName.Text);
this.lblDirectoryName.Text = Path.GetDirectoryName(this.txtPathName.Text);
this.lblFileName.Text = Path.GetFileName(this.txtPathName.Text);
this.lblFileNameWithoutExtension.Text = Path.GetFileNameWithoutExtension(this.txtPathName.Text);
this.lblExtension.Text = Path.GetExtension(this.txtPathName.Text);
this.lblTemporaryPath.Text = Path.GetTempPath();
this.lblDirectorySeparatorChar.Text = Path.DirectorySeparatorChar.ToString();
this.lblAltDirectorySeparatorChar.Text = Path.AltDirectorySeparatorChar.ToString();
this.lblVolumeSeparatorChar.Text = Path.VolumeSeparatorChar.ToString();
this.lblPathSeparator.Text = Path.PathSeparator.ToString();
this.lblInvalidChars.Text = HttpUtility.HtmlEncode( new String(Path.GetInvalidPathChars() ) );
this.lblInvalidFileNameChars.Text = HttpUtility.HtmlEncode( new String(Path.GetInvalidFileNameChars()));
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
Working with the Path Class<br />
<br />
Enter a path name:
<asp:TextBox ID="txtPathName" runat="server"/><br />
<asp:Button ID="Button1" runat="server" Text="Button" /><br />
<br />
Root Path =
<asp:Label ID="lblRootPath" runat="server" Text="Label" />
<br />
Directory =
<asp:Label ID="lblDirectoryName" runat="server" Text="Label" />
<br />
Filename =
<asp:Label ID="lblFileName" runat="server" Text="Label" />
<br />
Filename (without extension) =
<asp:Label ID="lblFileNameWithoutExtension" runat="server" Text="Label" />
<br />
Extension =
<asp:Label ID="lblExtension" runat="server" Text="Label" />
<br />
<br />
Temporary Directory =
<asp:Label ID="lblTemporaryPath" runat="server" Text="Label" />
<br />
Directory Separator Character =
<asp:Label ID="lblDirectorySeparatorChar" runat="server" Text="Label" />
<br />
Alt Directory Separator Character =
<asp:Label ID="lblAltDirectorySeparatorChar" runat="server" Text="Label" />
<br />
Volume Separator Character =
<asp:Label ID="lblVolumeSeparatorChar" runat="server" Text="Label" />
<br />
Path Separator Character =
<asp:Label ID="lblPathSeparator" runat="server" Text="Label" />
<br />
Invalid Path Characters =
<asp:Label ID="lblInvalidChars" runat="server" Text="Label" />
<br />
Invalid FileName Characters =
<asp:Label ID="lblInvalidFileNameChars" runat="server" Text="Label" />
</div>
</form>
</body>
</html>
Using the Path class (VB)
<%@ Page Language="VB" %>
<%@ Import Namespace="System.IO" %>
<!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)
If Page.IsPostBack Then
Me.lblRootPath.Text = Path.GetPathRoot(Me.txtPathName.Text)
Me.lblDirectoryName.Text = Path.GetDirectoryName(Me.txtPathName.Text)
Me.lblFileName.Text = Path.GetFileName(Me.txtPathName.Text)
Me.lblFileNameWithoutExtension.Text = Path.GetFileNameWithoutExtension(Me.txtPathName.Text)
Me.lblExtension.Text = Path.GetExtension(Me.txtPathName.Text)
Me.lblTemporaryPath.Text = Path.GetTempPath()
Me.lblDirectorySeparatorChar.Text = Path.DirectorySeparatorChar.ToString()
Me.lblAltDirectorySeparatorChar.Text = Path.AltDirectorySeparatorChar.ToString()
Me.lblVolumeSeparatorChar.Text = Path.VolumeSeparatorChar.ToString()
Me.lblPathSeparator.Text = Path.PathSeparator.ToString()
Me.lblInvalidChars.Text = HttpUtility.HtmlEncode(New String(Path.GetInvalidPathChars()))
Me.lblInvalidFileNameChars.Text = HttpUtility.HtmlEncode(New String(Path.GetInvalidFileNameChars()))
End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
Working with the Path Class<br />
<br />
Enter a path name:
<asp:TextBox ID="txtPathName" runat="server"></asp:TextBox><br />
<asp:Button ID="Button1" runat="server" Text="Button" /><br />
<br />
Root Path =
<asp:Label ID="lblRootPath" runat="server" Text="Label" />
<br />
Directory =
<asp:Label ID="lblDirectoryName" runat="server" Text="Label" />
<br />
Filename =
<asp:Label ID="lblFileName" runat="server" Text="Label" />
<br />
Filename (without extension) =
<asp:Label ID="lblFileNameWithoutExtension" runat="server" Text="Label" />
<br />
Extension =
<asp:Label ID="lblExtension" runat="server" Text="Label" />
<br />
<br />
Temporary Directory =
<asp:Label ID="lblTemporaryPath" runat="server" Text="Label" />
<br />
Directory Separator Character =
<asp:Label ID="lblDirectorySeparatorChar" runat="server" Text="Label" />
<br />
Alt Directory Separator Character =
<asp:Label ID="lblAltDirectorySeparatorChar" runat="server" Text="Label" />
<br />
Volume Separator Character =
<asp:Label ID="lblVolumeSeparatorChar" runat="server" Text="Label" />
<br />
Path Separator Character =
<asp:Label ID="lblPathSeparator" runat="server" Text="Label" />
<br />
Invalid Path Characters =
<asp:Label ID="lblInvalidChars" runat="server" Text="Label" />
<br />
Invalid FileName Characters =
<asp:Label ID="lblInvalidFileNameChars" runat="server" Text="Label" />
</div>
</form>
</body>
</html>