ASP.NET Tutorial/File Directory/Path
Содержание
File Path Parsing (C#)
<source lang="csharp">
<%@ 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("
Full Path | "); PropertyContainer.Append("" + Path.GetFullPath(FileNameTextBox.Text) + " |
Root Drive | "); PropertyContainer.Append("" + Path.GetPathRoot(FileNameTextBox.Text) + " |
Directory Path | "); PropertyContainer.Append("" + Path.GetDirectoryName(FileNameTextBox.Text) + " |
File Name | "); PropertyContainer.Append("" + Path.GetFileName(FileNameTextBox.Text) + " |
File Name (Without Extension) | "); PropertyContainer.Append("" + Path.GetFileNameWithoutExtension(FileNameTextBox.Text) + " |
File Extension | "); PropertyContainer.Append("" + Path.GetExtension(FileNameTextBox.Text) + " |
Temporary Filename | "); PropertyContainer.Append("" + Path.GetTempFileName() + " |
Temprary Filepath | "); PropertyContainer.Append("" + Path.GetTempPath() + " |
Directory Separator | "); PropertyContainer.Append("" + Path.DirectorySeparatorChar + " |
Alt Directory Separator | "); PropertyContainer.Append("" + Path.AltDirectorySeparatorChar + " |
Path Separator | "); PropertyContainer.Append("" + Path.PathSeparator + " |
Volume Separator | "); PropertyContainer.Append("" + Path.VolumeSeparatorChar + " |
Invalid Path Characters | "); PropertyContainer.Append("" + Path.InvalidPathChars + " |
PathPropertiesLiteral.Text = PropertyContainer.ToString(); }
</script> <html>
<head> <title>File Path Parsing</title> </head> <body> <form runat="server">
File Path Parser
<asp:TextBox id="FileNameTextBox" runat="server"></asp:TextBox> <asp:Button id="ParsePathButton" onclick="ParsePath" runat="server" Text="Parse"></asp:Button>
</form> <asp:Literal id="PathPropertiesLiteral" runat="server"></asp:Literal> </body>
</html></source>
File Path Parsing (VB)
<source lang="csharp">
<%@ 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("
Full Path | ") .Append("" & Path.GetFullPath(FileNameTextBox.Text) & " |
Root Drive | ") .Append("" & Path.GetPathRoot(FileNameTextBox.Text) & " |
Directory Path | ") .Append("" & Path.GetDirectoryName(FileNameTextBox.Text) & " |
File Name | ") .Append("" & Path.GetFileName(FileNameTextBox.Text) & " |
File Name (Without Extension) | ") .Append("" & Path.GetFileNameWithoutExtension(FileNameTextBox.Text) & " |
File Extension | ") .Append("" & Path.GetExtension(FileNameTextBox.Text) & " |
Temporary Filename | ") .Append("" & Path.GetTempFileName() & " |
Temprary Filepath | ") .Append("" & Path.GetTempPath() & " |
Directory Separator | ") .Append("" & Path.DirectorySeparatorChar & " |
Alt Directory Separator | ") .Append("" & Path.AltDirectorySeparatorChar & " |
Path Separator | ") .Append("" & Path.PathSeparator & " |
Volume Separator | ") .Append("" & Path.VolumeSeparatorChar & " |
Invalid Path Characters | ") .Append("" & Path.InvalidPathChars & " |
End With PathPropertiesLiteral.Text = PropertyContainer.ToString End Sub
</script> <html>
<head> <title>CookBook :: File Path Parsing</title> </head> <body> <form runat="server">
File Path Parser
<asp:TextBox id="FileNameTextBox" runat="server"></asp:TextBox> <asp:Button id="ParsePathButton" onclick="ParsePath" runat="server" Text="Parse"></asp:Button>
</form> <asp:Literal id="PathPropertiesLiteral" runat="server"></asp:Literal> </body>
</html></source>
Using the Path class (C#)
<source lang="csharp">
<%@ 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">
Working with the Path Class
Enter a path name: <asp:TextBox ID="txtPathName" runat="server"/>
<asp:Button ID="Button1" runat="server" Text="Button" />
Root Path = <asp:Label ID="lblRootPath" runat="server" Text="Label" />
Directory = <asp:Label ID="lblDirectoryName" runat="server" Text="Label" />
Filename = <asp:Label ID="lblFileName" runat="server" Text="Label" />
Filename (without extension) = <asp:Label ID="lblFileNameWithoutExtension" runat="server" Text="Label" />
Extension = <asp:Label ID="lblExtension" runat="server" Text="Label" />
Temporary Directory = <asp:Label ID="lblTemporaryPath" runat="server" Text="Label" />
Directory Separator Character = <asp:Label ID="lblDirectorySeparatorChar" runat="server" Text="Label" />
Alt Directory Separator Character = <asp:Label ID="lblAltDirectorySeparatorChar" runat="server" Text="Label" />
Volume Separator Character = <asp:Label ID="lblVolumeSeparatorChar" runat="server" Text="Label" />
Path Separator Character = <asp:Label ID="lblPathSeparator" runat="server" Text="Label" />
Invalid Path Characters = <asp:Label ID="lblInvalidChars" runat="server" Text="Label" />
Invalid FileName Characters = <asp:Label ID="lblInvalidFileNameChars" runat="server" Text="Label" />
</form>
</body> </html></source>
Using the Path class (VB)
<source lang="csharp">
<%@ 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">
Working with the Path Class
Enter a path name: <asp:TextBox ID="txtPathName" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" />
Root Path = <asp:Label ID="lblRootPath" runat="server" Text="Label" />
Directory = <asp:Label ID="lblDirectoryName" runat="server" Text="Label" />
Filename = <asp:Label ID="lblFileName" runat="server" Text="Label" />
Filename (without extension) = <asp:Label ID="lblFileNameWithoutExtension" runat="server" Text="Label" />
Extension = <asp:Label ID="lblExtension" runat="server" Text="Label" />
Temporary Directory = <asp:Label ID="lblTemporaryPath" runat="server" Text="Label" />
Directory Separator Character = <asp:Label ID="lblDirectorySeparatorChar" runat="server" Text="Label" />
Alt Directory Separator Character = <asp:Label ID="lblAltDirectorySeparatorChar" runat="server" Text="Label" />
Volume Separator Character = <asp:Label ID="lblVolumeSeparatorChar" runat="server" Text="Label" />
Path Separator Character = <asp:Label ID="lblPathSeparator" runat="server" Text="Label" />
Invalid Path Characters = <asp:Label ID="lblInvalidChars" runat="server" Text="Label" />
Invalid FileName Characters = <asp:Label ID="lblInvalidFileNameChars" runat="server" Text="Label" />
</form>
</body> </html></source>