ASP.Net/File Directory/FileInfo
Содержание
Copying or Moving a File (C#)
<source lang="csharp">
<%@ Page Language="C#" %> <script runat="server"> void CopyFile_Click(object sender, System.EventArgs e) {
try { string sourceFile = Server.MapPath("Data.txt"); string destinationFile = Server.MapPath("Datacopy.txt"); System.IO.File.Copy(sourceFile, destinationFile); } catch (Exception ex) { MessageLabel.Text = ex.Message; }
} void MoveFile_Click(object sender, System.EventArgs e) {
try { string sourceFile = Server.MapPath("Data.txt"); string destinationFile = Server.MapPath("Datamove.txt"); System.IO.File.Move(sourceFile, destinationFile); } catch (Exception ex) { MessageLabel.Text = ex.Message; }
} </script> <html>
<head> <title>Copying or Moving a File</title> </head> <body> <form id="MainForm" runat="server"> Copy "Data.txt" to "Datacopy.txt" <asp:button id="CopyFile" runat="server" text="Copy File" onclick="CopyFile_Click" />
Move "Data.txt" to "Datamove.txt" <asp:button id="MoveFile" runat="server" text="Move File" onclick="MoveFile_Click" />
<asp:Label ID="MessageLabel" Runat="server" /> </form> </body>
</html>
</source>
Copying or Moving a File (VB)
<source lang="csharp">
<%@ Page Language="VB" %> <script runat="server"> Sub CopyFile_Click(sender As Object, e As System.EventArgs)
Try Dim sourceFile As String = Server.MapPath("Data.txt") Dim destinationFile As String = Server.MapPath("Datacopy.txt") System.IO.File.Copy(sourceFile, destinationFile) Catch Ex As Exception MessageLabel.Text = Ex.Message End Try
End Sub Sub MoveFile_Click(sender As Object, e As System.EventArgs)
Try Dim sourceFile As String = Server.MapPath("Data.txt") Dim destinationFile As String = Server.MapPath("Datamove.txt") System.IO.File.Move(sourceFile, destinationFile) Catch Ex As Exception MessageLabel.Text = Ex.Message End Try
End Sub </script> <html>
<head> <title>Copying or Moving a File</title> </head> <body> <form id="MainForm" runat="server"> Copy "Data.txt" to "Datacopy.txt" <asp:button id="CopyFile" runat="server" text="Copy File" onclick="CopyFile_Click" />
Move "Data.txt" to "Datamove.txt" <asp:button id="MoveFile" runat="server" text="Move File" onclick="MoveFile_Click" />
<asp:Label ID="MessageLabel" Runat="server" /> </form> </body>
</html>
</source>
Inquiring Information About a File (C#)
<source lang="csharp">
<%@ Page Language="C#" %> <%@ Import Namespace="System.IO" %> <%@ Import Namespace="System" %> <script runat="server">
void Page_Load(object Source, EventArgs e) { ListFileInfoLiteral.Text = ""; FileTextBox.Text = Server.MapPath("Default.aspx"); } public void DisplayFileInfo(object Source, EventArgs e) { FileInfo iFile; iFile = new FileInfo(FileTextBox.Text); if (iFile.Exists){ System.Text.StringBuilder sb = new System.Text.StringBuilder(1000);sb.Append( "
Name | "); sb.Append( "" + iFile.Name + " | "); sb.Append( "
Path\\Name | "); sb.Append( "" + iFile.FullName + " | "); sb.Append( "
Extension | "); sb.Append( "" + iFile.Extension + " | "); sb.Append( "
Size | "); sb.Append( "" + iFile.Length + " bytes | "); sb.Append( "
Attributes | "); sb.Append( "" + iFile.Attributes.ToString() + " | "); sb.Append( "
Creation Time | "); sb.Append( "" + iFile.CreationTime + " | "); sb.Append( "
Last Accessed | "); sb.Append( "" + iFile.LastAccessTime + " | "); sb.Append( "
Last Modified | "); sb.Append( "" + iFile.LastWriteTime + " | "); sb.Append( "
ListFileInfoLiteral.Text = sb.ToString(); } else ListFileInfoLiteral.Text = "The file does not exist"; }
</script> <HTML>
<HEAD> <title>Inquiring Information About a File</title> </HEAD> <body> <form runat="server">
Type path\name of file and click the button.
<asp:TextBox id="FileTextBox" runat="server" ReadOnly="True"></asp:TextBox> <asp:Button id="GetFileInfoButton" onclick="DisplayFileInfo" runat="server" Text="File Information"></asp:Button>
<asp:Literal id="ListFileInfoLiteral" runat="server"></asp:Literal>
</form> </body>
</HTML>
</source>
Inquiring Information About a File (VB)
<source lang="csharp">
<%@ Page Language="VB" %> <%@ Import Namespace="System.IO" %> <%@ Import Namespace="System" %> <script runat="server">
Public Sub Page_Load(Source As Object, Sender As EventArgs) ListFileInfoLiteral.Text = "" FileTextBox.Text = Server.MapPath("Default.aspx") End Sub Public Sub DisplayFileInfo(Source As Object, Sender As EventArgs) Dim iFile As FileInfo iFile = New FileInfo(FileTextBox.Text) If iFile.Exists Then Dim sb As New System.Text.StringBuilder(1000)sb.Append("
Name | ") sb.Append("" & iFile.Name & " | ") sb.Append("
Path\Name | ") sb.Append("" & iFile.FullName & " | ") sb.Append("
Extension | ") sb.Append("" & iFile.Extension & " | ") sb.Append("
Size | ") sb.Append("" & iFile.Length & " bytes | ") sb.Append("
Attributes | ") sb.Append("" & iFile.Attributes.ToString() & " | ") sb.Append("
Creation Time | ") sb.Append("" & iFile.CreationTime & " | ") sb.Append("
Last Accessed | ") sb.Append("" & iFile.LastAccessTime & " | ") sb.Append("
Last Modified | ") sb.Append("" & iFile.LastWriteTime & " | ") sb.Append("
ListFileInfoLiteral.Text = sb.ToString() Else ListFileInfoLiteral.Text = "The file does not exist" End If End Sub
</script> <HTML>
<HEAD> <title>Inquiring Information About a File</title> </HEAD> <body> <form runat="server">
Type path\name of file and click the button.
<asp:TextBox id="FileTextBox" runat="server" ReadOnly="True"></asp:TextBox> <asp:Button id="GetFileInfoButton" onclick="DisplayFileInfo" runat="server" Text="File Information"></asp:Button>
<asp:Literal id="ListFileInfoLiteral" runat="server"></asp:Literal>
</form> </body>
</HTML>
</source>