文件的删除和文件信息的读取
<table border="1">
<tr>
<td colspan="2" style="color: #660066;">
删除文件:
</td>
</tr>
<tr>
<td style="font-size: 10pt">
请输入要删除文件的路径:
</td>
<td style="width: 158px">
<asp:TextBox ID="FileTextBox" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Label ID="MsgLabel" runat="server" ForeColor="red"></asp:Label>
</td>
</tr>
<tr>
<td align="center" colspan="2">
<asp:Button ID="ExistButton" runat="server" Text="删除" OnClick="ExistButton_Click" />
</td>
</tr>
</table>
using System.IO;
public partial class Delete : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void ExistButton_Click(object sender, EventArgs e)
{
try
{
if (!File.Exists(FileTextBox.Text))
{
MsgLabel.Text = "该文件不存在";
}
else
{
File.Delete(FileTextBox.Text);
MsgLabel.Text = "删除文件成功!";
}
}
catch (Exception ee)
{
MsgLabel.Text = "操作失败! 失败的原因是:" + ee.ToString();
}
}
}
2:读取文件详细信息:
<table border="1">
<tr>
<td colspan="2" style="color: #660066;">
获取文件的基本信息:
</td>
</tr>
<tr>
<td style="font-size: 10pt">
请输入要查看文件的路径:
</td>
<td style="width: 158px">
<asp:TextBox ID="FileNameTextBox" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Label ID="MsgLabel" runat="server" ForeColor="red"></asp:Label><br/>
<asp:Label ID="DirLabel" runat="server"></asp:Label><br/>
<asp:Label ID="LengthLabel" runat="server" Text="Label"></asp:Label><br/>
<asp:Label ID="CreationTimeLabel" runat="server" Text="Label"></asp:Label><br/>
<asp:Label ID="LastWriteTimeLabel" runat="server" Text="Label"></asp:Label><br/>
<asp:Label ID="AttributesLabel" runat="server" Text="Label"></asp:Label>
</td>
</tr>
<tr>
<td align="center" colspan="2">
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="确定" /></td>
</tr>
</table>
using System.IO;
public partial class FileInfo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
System.IO.FileInfo fi1 = new System.IO.FileInfo(FileNameTextBox.Text);
if (!fi1.Exists)
{
MsgLabel.Text = "该文件不存在!";
DirLabel.Visible = false;
LengthLabel.Visible = false;
CreationTimeLabel.Visible = false;
LastWriteTimeLabel.Visible = false;
AttributesLabel.Visible = false;
}
else
{
MsgLabel.Text = "获取文件信息成功!内容如下:";
DirLabel.Visible = true;
LengthLabel.Visible = true;
CreationTimeLabel.Visible = true;
LastWriteTimeLabel.Visible = true;
AttributesLabel.Visible = true;
DirLabel.Text = "文件所在位置:" + fi1.DirectoryName;
LengthLabel.Text = "文件大小:" + fi1.Length + "字节";
CreationTimeLabel.Text = "文件创建时间:" + fi1.CreationTime;
LastWriteTimeLabel.Text = "文件最近修改时间:" + fi1.LastWriteTime;
AttributesLabel.Text = "文件属性:" + fi1.Attributes;
}
}
catch (Exception ee)
{
MsgLabel.Text = "获取文件信息失败!失败原因:" + ee.ToString();
}
}
}