以文件存储显示客户留言

 1         ArrayList list = new ArrayList();
 2         protected void Page_Load(object sender, EventArgs e)
 3         {
 4             if (!IsPostBack)
 5             {
 6                 //读取文本
 7                 string strFile = Server.MapPath("") + "\\" + "msg.txt";
 8                 StreamReader sr = new StreamReader(strFile, System.Text.Encoding.Default);
 9                 list.Clear();
10                 //遍历文本
11                 while (sr.Peek() > 0)
12                 {
13                     list.Add(sr.ReadLine());
14                 }
15                 sr.Dispose();
16                 sr.Close();
17                 DataRow dr;
18                 DataTable dt = new DataTable();
19                 //列名添加
20                 dt.Columns.Add("11", typeof(string));
21                 dt.Columns.Add("22", typeof(string));
22                 
23                 if (list.Count > 0)
24                 {
25                     string[] myarray;
26                     foreach (string s in list)
27                     {
28                         //以‘|’分割
29                         myarray = s.Split('|');
30                         dr = dt.NewRow();
31                         dr["11"] = myarray[0].ToString();
32                         dr["22"] = myarray[1].ToString();
33                         dt.Rows.Add(dr);
34                     }
35                 }
36                 GridView1.DataSource = dt.DefaultView;
37                 GridView1.DataBind();
38             }
39         }
40 
41         protected void btn_Click(object sender, EventArgs e)
42         {
43             //打开文本
44             string strFile = Server.MapPath("") + "\\" + "msg.txt";
45             StreamWriter sw = new StreamWriter(strFile, true, System.Text.Encoding.GetEncoding("gb2312"));
46             //文本写入
47             sw.WriteLine(txt.Text);
48             sw.Flush();
49             sw.Dispose();
50             sw.Close();         
51         }


 

 

转载于:https://www.cnblogs.com/X-Jonney/archive/2013/05/25/3098541.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值