ASP.NET Tutorial/File Directory/Path

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

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(""); PropertyContainer.Append(""); PropertyContainer.Append(""); PropertyContainer.Append(""); PropertyContainer.Append(""); PropertyContainer.Append(""); PropertyContainer.Append(""); PropertyContainer.Append(""); PropertyContainer.Append(""); PropertyContainer.Append(""); PropertyContainer.Append(""); PropertyContainer.Append(""); PropertyContainer.Append(""); PropertyContainer.Append(""); PropertyContainer.Append(""); PropertyContainer.Append(""); PropertyContainer.Append(""); PropertyContainer.Append(""); PropertyContainer.Append(""); PropertyContainer.Append(""); PropertyContainer.Append(""); PropertyContainer.Append(""); PropertyContainer.Append(""); PropertyContainer.Append(""); PropertyContainer.Append(""); PropertyContainer.Append(""); PropertyContainer.Append(""); PropertyContainer.Append("
Full Path" + Path.GetFullPath(FileNameTextBox.Text) + "
Root Drive" + Path.GetPathRoot(FileNameTextBox.Text) + "
Directory Path" + Path.GetDirectoryName(FileNameTextBox.Text) + "
File Name" + Path.GetFileName(FileNameTextBox.Text) + "
File Name (Without Extension)" + Path.GetFileNameWithoutExtension(FileNameTextBox.Text) + "
File Extension" + Path.GetExtension(FileNameTextBox.Text) + "
Temporary Filename" + Path.GetTempFileName() + "
Temprary Filepath" + Path.GetTempPath() + "
Directory Separator" + Path.DirectorySeparatorChar + "
Alt Directory Separator" + Path.AltDirectorySeparatorChar + "
Path Separator" + Path.PathSeparator + "
Volume Separator" + Path.VolumeSeparatorChar + "
Invalid Path Characters" + 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("") .Append("") .Append("") .Append("") .Append("") .Append("") .Append("") .Append("") .Append("") .Append("") .Append("") .Append("") .Append("") .Append("") .Append("") .Append("") .Append("") .Append("") .Append("") .Append("") .Append("") .Append("") .Append("") .Append("") .Append("") .Append("") .Append("") .Append("
Full Path" & Path.GetFullPath(FileNameTextBox.Text) & "
Root Drive" & Path.GetPathRoot(FileNameTextBox.Text) & "
Directory Path" & Path.GetDirectoryName(FileNameTextBox.Text) & "
File Name" & Path.GetFileName(FileNameTextBox.Text) & "
File Name (Without Extension)" & Path.GetFileNameWithoutExtension(FileNameTextBox.Text) & "
File Extension" & Path.GetExtension(FileNameTextBox.Text) & "
Temporary Filename" & Path.GetTempFileName() & "
Temprary Filepath" & Path.GetTempPath() & "
Directory Separator" & Path.DirectorySeparatorChar & "
Alt Directory Separator" & Path.AltDirectorySeparatorChar & "
Path Separator" & Path.PathSeparator & "
Volume Separator" & Path.VolumeSeparatorChar & "
Invalid Path Characters" & 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>