using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("Name", typeof(string)));
            string serverPath = Server.MapPath("Files");
            DirectoryInfo dir = new DirectoryInfo(serverPath);
            foreach (FileInfo fileName in dir.GetFiles())
            {
                DataRow dr = dt.NewRow();
                dr[0] = fileName;
                dt.Rows.Add(dr);
            }
            DropDownList1.DataSource = dt;
            DropDownList1.DataTextField = "Name";
            DropDownList1.DataValueField = "Name";
            DropDownList1.DataBind();
            string path = Server.MapPath("Files/") + DropDownList1.SelectedValue.ToString();
            StreamReader sr = File.OpenText(path);
            while (sr.Peek() != -1)
            {
                string str = sr.ReadLine() + "\n";
                TextBox1.Text += str;
            }
            sr.Close();
        }
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        TextBox1.Text = "";
        StreamReader sr = File.OpenText(Server.MapPath("Files/") + DropDownList1.SelectedValue.ToString());
        while (sr.Peek() != -1)
        {
            string str = sr.ReadLine() + "\n";
            TextBox1.Text += str;
        }
        sr.Close();    
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        StreamWriter fw = new StreamWriter(Server.MapPath("Files/") + DropDownList1.SelectedValue.ToString());
        fw.Write(TextBox1.Text);
        fw.Close();
        Response.Write("<script>alert('成功修改了文件中的内容!')</script>");
    }
}
