asp.net 4.5 práctica ~ test14-6 streamwrite escribir archivos

webform1.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="test14_6.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></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h3>添加新词条</h3>
        <asp:Label ID="Label2" runat="server" Text="标题"></asp:Label>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
        <asp:Label ID="Label1" runat="server" Text="描述:"></asp:Label>
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
        <asp:Button ID="Button1" runat="server" Text="保存内容" OnClick="Button1_Click" />
        <asp:Label ID="lblTips" runat="server" Text=""></asp:Label>
    </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 test14_6
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string path = Server.MapPath("test.txt");
            string title = TextBox1.Text;
            string word = TextBox2.Text;

            System.IO.FileInfo file = new System.IO.FileInfo(path);
            if (file.Exists == false)
            {
                System.IO.StreamWriter write1 = file.CreateText();
                write1.WriteLine(title);
                write1.WriteLine(word);
                write1.Flush();
                write1.Close();
                lblTips.Text = "创建文件并写入数据成功!";
            }
        }
    }
}

 

Supongo que te gusta

Origin blog.csdn.net/modern358/article/details/114804311
Recomendado
Clasificación