ASP.NET Tutorial/File Directory/Directory — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 18:30, 26 мая 2010
Содержание
- 1 AutoPostBack is true
- 2 Get directory information (VB.net)
- 3 Get file names under a directory
- 4 Setting and displaying the application"s working directory (C#)
- 5 Setting and displaying the application"s working directory (VB)
- 6 static methods of the Directory class (C#)
- 7 static methods of the Directory class (VB)
AutoPostBack is true
<source lang="csharp">
<%@ 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">
Sub txtSearch_TextChanged(ByVal sender As Object, ByVal e As EventArgs) lblSearchResults.Text = "Search for: " & txtSearch.Text End Sub
</script> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server">
<title>TextBox AutoPostBack</title>
</head> <body>
<form id="form1" runat="server">
<asp:Label id="lblSearch" Text="Search:" Runat="server" /> <asp:TextBox id="txtSearch" AutoPostBack="true" OnTextChanged="txtSearch_TextChanged" Runat="server" /> <asp:Label id="lblSearchResults" Runat="server" />
</form>
</body> </html></source>
Get directory information (VB.net)
<source lang="csharp">
<%@ Import Namespace="System.IO" %> <script language="VB" runat="server">
sub Page_Load(Sender as Object, e as EventArgs) dim f as new FileInfo(Server.MapPath("test.aspx")) dim dir as DirectoryInfo = f.Directory lblMessage.Text += "Directory information
" & _ "Name: " & dir.Name & "
" & _ "Full name: " & dir.FullName & "
" & _ "Last access time: " & dir.LastAccessTime & _ "
" & _ "Last write time: " & dir.LastWriteTime & _ "
" & _ "Parent: " & dir.Parent.Name & "
" end sub
</script> <html><body>
<asp:label id="lblMessage" runat=server/>
</body></html></source>
Get file names under a directory
<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">
Sub Page_Load() lnkRandom.NavigateUrl = GetRandomFile() End Sub Function GetRandomFile() As String Dim files As String() = Directory.GetFiles(MapPath(Request.ApplicationPath), "*.aspx") Dim rnd As New Random() Dim rndFile As String = files(rnd.Next(files.Length)) Return Path.GetFileName(rndFile) End Function
</script> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server">
<title>Show HyperLink</title>
</head> <body>
<form id="form1" runat="server">
<asp:HyperLink id="lnkRandom" Text="Random Link" Runat="server" />
</form>
</body> </html></source>
Setting and displaying the application"s working directory (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) { this.Label1.Text = Directory.GetCurrentDirectory(); Directory.SetCurrentDirectory(@"C:\"); this.Label2.Text = Directory.GetCurrentDirectory(); }
</script> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">
<title>Untitled Page</title>
</head> <body>
<form id="form1" runat="server">
Old Working Directory: <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
New Working Directory: <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
</form>
</body> </html></source>
Setting and displaying the application"s working directory (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) Me.Label1.Text = Directory.GetCurrentDirectory() Directory.SetCurrentDirectory("C:\") Me.Label2.Text = Directory.GetCurrentDirectory() 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">
Old Working Directory: <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
New Working Directory: <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
</form>
</body> </html></source>
static methods of the Directory 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) { Directory.CreateDirectory(@"C:\"); if (Directory.Exists(@"C:\") ) { this.Label1.Text = Directory.GetCreationTime(@"C:\").ToString(); this.Label2.Text = Directory.GetLastAccessTime(@"C:\").ToString(); this.Label3.Text = Directory.GetLastWriteTime(@"C:\").ToString(); Directory.Delete(@"C:\"); } }
</script> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">
<title>Untitled Page</title>
</head> <body>
<form id="form1" runat="server">
Creation Time: <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
Last Access Time: <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
Last Write Time: <asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
</form>
</body> </html></source>
static methods of the Directory 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) Directory.CreateDirectory("C:\") If Directory.Exists("C:\") Then Me.Label1.Text = Directory.GetCreationTime("C:\").ToString() Me.Label2.Text = Directory.GetLastAccessTime("C:\").ToString() Me.Label3.Text = Directory.GetLastWriteTime("C:\").ToString() Directory.Delete("C:\") 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">
Creation Time: <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
Last Access Time: <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
Last Write Time: <asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
</form>
</body> </html></source>