以文件存储显示客户留言

 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         }


 

 

你可能感兴趣的:(文件)