asp.net代码练习 work072

258 篇文章 2 订阅
这是一个C# ASP.NET应用程序的示例,展示了如何使用FileInfo类创建、写入、删除文件,并列出目录中所有.txt文件的信息。用户通过网页按钮触发操作,如创建10个.txt文件,向每个文件写入特定文本,以及删除这些文件。此外,程序还会显示文件的名称、创建时间、大小和全路径。
摘要由CSDN通过智能技术生成

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="创建文件" OnClick="btnCreateFile_Click" />
                </td>
                <td>
                    <asp:Button ID="btnWriteFile" runat="server" Text="写入内容" OnClick="btnWriteFile_Click" />
                </td>
                <td>
                    <asp:Button ID="btnDeleteFile" runat="server" Text="删除文件" OnClick="btnDeleteFile_Click" />
                </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>");
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

虾米大王

有你的支持,我会更有动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值