asp.net代码练习 work072
webform1.aspx<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="work072.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>FileInfo类的示例</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table border="0">
<tr>
<td>
<asp:Button ID="btnCreateFile" runat="server" Text="创建文件"/>
</td>
<td>
<asp:Button ID="btnWriteFile" runat="server" Text="写入内容"/>
</td>
<td>
<asp:Button ID="btnDeleteFile" runat="server" Text="删除文件"/>
</td>
</tr>
</table>
<table border="1">
<tr>
<td>文件名</td>
<td>创建时间</td>
<td>文件大小</td>
<td>全路径</td>
</tr>
<% ListAllFile(); %>
</table>
</div>
</form>
</body>
</html>
webform1.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace work072
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
/// <summary>
/// 创建文件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnCreateFile_Click(object sender, EventArgs e)
{
string path = Server.MapPath("~");
for (int i = 0; i < 10; i++)
{
System.IO.FileStream stream = System.IO.File.Create(path + (i +1) + ".txt");
stream.Close();
}
}
/// <summary>
/// 写入文件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnWriteFile_Click(object sender, EventArgs e)
{
string path = Server.MapPath("~");
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(path);
System.IO.FileInfo[] files = dir.GetFiles("*.txt");
System.IO.StreamWriter writer = null;
foreach (System.IO.FileInfo f in files)
{
writer = f.AppendText();
//一个汉字占3个字节,一个字母占1个字节,行位符占2个字节
writer.WriteLine("虾米大王教你学编程系列之ASP.NET中级教程");
writer.Close();
}
}
/// <summary>
/// 删除文件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnDeleteFile_Click(object sender, EventArgs e)
{
string path = Server.MapPath("~");
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(path);
System.IO.FileInfo[] files = dir.GetFiles("*.txt");
System.IO.StreamWriter writer = null;
foreach (System.IO.FileInfo f in files)
{
f.Delete();
}
}
/// <summary>
/// 显示文件信息
/// </summary>
protected void ListAllFile()
{
string path = Server.MapPath("~");
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(path);
System.IO.FileInfo[] files = dir.GetFiles("*.txt");
foreach (System.IO.FileInfo f in files)
{
Response.Write("<tr>");
Response.Write(string.Format("<td>{0}</td>",f.Name));
Response.Write(string.Format("<td>{0}</td>", f.CreationTime));
Response.Write(string.Format("<td>{0} 字节</td>", f.Length));
Response.Write(string.Format("<td>{0}</td>", f.FullName));
Response.Write("</tr>");
}
}
}
}
https://blog.51cto.com/u_15356972/3775777
页:
[1]