asp.net 读取和写入中文TXT文本

asp.net_读取和写入中文TXT文本



    protected void Page_Load(object sender, EventArgs e)

    {

        if (!IsPostBack)  ///这个必须要加啊  不然你保存的时候 他就读取了 
        {


            string path = Server.MapPath("../作者列表.txt");
            string txt = File.ReadAllText(path, System.Text.Encoding.GetEncoding("utf-8"));
            zuozhe.Text = txt;



        }



    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string path = Server.MapPath("../作者列表.txt");
        StreamWriter sw = new StreamWriter(path);
        sw.Write(zuozhe.Text, System.Text.Encoding.GetEncoding("utf-8"));//TextBox2中的bai文本是du可以编辑后的。zhi
        sw.Close();
        sw.Dispose();


        Response.Write("<script>alert('保存完成!');location.href='zuozhe.aspx';</script>");
    }

}




前台:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="zuozhe.aspx.cs" Inherits="admin_zuozhe" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">


<style type="text/css">

<!--
body {
font-family: Arial, Helvetica, sans-serif;
font-size:15px;
color:#666666;
background:#fff;
text-align:center;
}
* {


}

a {
color:#1E7ACE;
text-decoration:none; 
}
a:hover {
color:#000;
text-decoration:underline;
}
h3 {
font-size:14px;
font-weight:bold;
}

pre,p {
color:#1E7ACE;
margin:4px;
}
input, select,textarea {
padding:1px;
margin:2px;
font-size:22px;
}
.buttom{
padding:1px 10px;
font-size:22px;
border:1px #1E7ACE solid;
background:#D0F0FF;
width:100%;
}
#formwrapper {
width:350px;

padding:20px;
text-align:left;
border:1px solid #A4CDF2;
}
fieldset {
padding:10px;
margin-top:5px;
border:1px solid #A4CDF2;
background:#fff;
}
fieldset legend {
color:#1E7ACE;
font-weight:bold;
padding:3px 20px 3px 20px;
border:1px solid #A4CDF2; 
background:#fff;
}
fieldset label {
float:left;
width:70px;
text-align:right;
padding:4px;
margin:1px;
}
fieldset div {
clear:left;
margin-bottom:5px;
}
.enter{ text-align:center;}
.clear {
clear:both;
}
-->
</style>
</head>
<body>
<!--把下面代码加到<body>与</body>之间-->
<div id="formwrapper">


  <div action="" method="post" name="apForm" id="apForm">
    <fieldset>
    <legend>作者列表</legend>



        <div>

<asp:TextBox ID="zuozhe" runat="server" Height="417px" Width="300" 
                TextMode="MultiLine"></asp:TextBox>
      <font color="#0099CC"><b>
            <br />
            *请把你需要的作者复制进来,或者输入进来吧!<br />
            <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="保存更新" 
                Width="302px" />
            <br />
            </b></font><br/>
    </div>


        </div>


    
    </div>
    </form>
</body>
</html>



webform
桂ICP备16004754号