ASP.Net/File Directory/FileInfo
Версия от 15:30, 26 мая 2010; (обсуждение)
Содержание
Copying or Moving a File (C#)
<%@ 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" />
<br />
Move "Data.txt" to "Datamove.txt"
<asp:button id="MoveFile" runat="server" text="Move File" onclick="MoveFile_Click" />
<br />
<asp:Label ID="MessageLabel" Runat="server" />
</form>
</body>
</html>
Copying or Moving a File (VB)
<%@ 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" />
<br />
Move "Data.txt" to "Datamove.txt"
<asp:button id="MoveFile" runat="server" text="Move File" onclick="MoveFile_Click" />
<br />
<asp:Label ID="MessageLabel" Runat="server" />
</form>
</body>
</html>
Inquiring Information About a File (C#)
<%@ 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( "<table border="0" width="50%">");
sb.Append( "<tr>");
sb.Append( "<td>Name</td>");
sb.Append( "<td>" + iFile.Name + "</td>");
sb.Append( "</tr><tr>");
sb.Append( "<td>Path\\Name</td>");
sb.Append( "<td>" + iFile.FullName + "</td>");
sb.Append( "</tr><tr>");
sb.Append( "<td>Extension</td>");
sb.Append( "<td>" + iFile.Extension + "</td>");
sb.Append( "</tr><tr>");
sb.Append( "<td>Size</td>");
sb.Append( "<td>" + iFile.Length + " bytes</td>");
sb.Append( "</tr><tr>");
sb.Append( "<td>Attributes</td>");
sb.Append( "<td>" + iFile.Attributes.ToString() + "</td>");
sb.Append( "</tr><tr>");
sb.Append( "<td>Creation Time</td>");
sb.Append( "<td>" + iFile.CreationTime + "</td>");
sb.Append( "</tr><tr>");
sb.Append( "<td>Last Accessed</td>");
sb.Append( "<td>" + iFile.LastAccessTime + "</td>");
sb.Append( "</tr><tr>");
sb.Append( "<td>Last Modified</td>");
sb.Append( "<td>" + iFile.LastWriteTime + "</td>");
sb.Append( "</tr></table>");
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">
<h4>Type path\name of file and click the button.
</h4>
<p>
<asp:TextBox id="FileTextBox" runat="server" ReadOnly="True"></asp:TextBox>
<asp:Button id="GetFileInfoButton" onclick="DisplayFileInfo" runat="server" Text="File Information"></asp:Button>
</p>
<hr align="left" width="50%">
<p>
<asp:Literal id="ListFileInfoLiteral" runat="server"></asp:Literal>
</p>
</form>
</body>
</HTML>
Inquiring Information About a File (VB)
<%@ 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("<table border="0" width="50%">")
sb.Append("<tr>")
sb.Append("<td>Name</td>")
sb.Append("<td>" & iFile.Name & "</td>")
sb.Append("</tr><tr>")
sb.Append("<td>Path\Name</td>")
sb.Append("<td>" & iFile.FullName & "</td>")
sb.Append("</tr><tr>")
sb.Append("<td>Extension</td>")
sb.Append("<td>" & iFile.Extension & "</td>")
sb.Append("</tr><tr>")
sb.Append("<td>Size</td>")
sb.Append("<td>" & iFile.Length & " bytes</td>")
sb.Append("</tr><tr>")
sb.Append("<td>Attributes</td>")
sb.Append("<td>" & iFile.Attributes.ToString() & "</td>")
sb.Append("</tr><tr>")
sb.Append("<td>Creation Time</td>")
sb.Append("<td>" & iFile.CreationTime & "</td>")
sb.Append("</tr><tr>")
sb.Append("<td>Last Accessed</td>")
sb.Append("<td>" & iFile.LastAccessTime & "</td>")
sb.Append("</tr><tr>")
sb.Append("<td>Last Modified</td>")
sb.Append("<td>" & iFile.LastWriteTime & "</td>")
sb.Append("</tr></table>")
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">
<h4>Type path\name of file and click the button.
</h4>
<p>
<asp:TextBox id="FileTextBox" runat="server" ReadOnly="True"></asp:TextBox>
<asp:Button id="GetFileInfoButton" onclick="DisplayFileInfo" runat="server" Text="File Information"></asp:Button>
</p>
<hr align="left" width="50%">
<p>
<asp:Literal id="ListFileInfoLiteral" runat="server"></asp:Literal>
</p>
</form>
</body>
</HTML>