读取文本文件2

 
 利用 DropDownList1控件制作一个文件下拉列表

1、在后台添加一个DropDownList1

2、using System.IO;


<style type="text/css">
     
     #Panel1 input
     {display:block; width:400px;}控制分行进行换行
      
      
      </style>

input是通过在运行状态下点击网页查看源代码

<div id="Panel1">
  
 <input name="ctl02" type="text" value="大飞多发点" /><input name="ctl03" type="text" value="更符合规范和看" /><input name="ctl04" type="text" value="后天发货同样" />
 </div>


<asp:DropDownList ID="DropDownList1" runat="server" >
    </asp:DropDownList>
    <asp:Button ID="Button1" runat="server" Text="Button" οnclick="Button1_Click" />
        <br />
    <asp:TextBox ID="TextBox1" runat="server"  Width="195px"  TextMode="MultiLine" 
            Rows="12"></asp:TextBox>
        
    
        <br />
        
    
    <asp:TextBox ID="TextBox2" runat="server" Width="200px"  TextMode="MultiLine" Rows="12"></asp:TextBox>
        <asp:Button ID="Button2" runat="server" Text="Button" οnclick="Button2_Click" />
    
    
        <asp:Button ID="Button3" runat="server" οnclick="Button3_Click" Text="Button" />
    
    
    </div>
    
    <asp:Panel ID="Panel1" runat="server">
    </asp:Panel>


后台代码

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string[] arrs = Directory.GetFiles(MapPath("txt"),"*.txt");//文件的地址,文件格式的选择条件
            //Response.Write(arrs.Length);输出文件的个数
            ListItem li;
            foreach (string s in arrs)
            {
                // Response.Write(s+"<br/>");输出文件的全路径如;F:\lab\txt\第一.txt
                //string a = Path.GetFileName(s);得到文件路径如:第一.txt(去掉磁盘位置)
               // Response.Write(a + "<br/>");
                li = new ListItem();
                string name = Path.GetFileNameWithoutExtension(s);//得到不带扩展名的文件名
                li.Text = name;
                DropDownList1.Items.Add(li);
            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string fname = DropDownList1.SelectedValue;
       //string path = MapPath("txt")+"\\"+fname;
      string path = Path.Combine(Server.MapPath("txt"),fname+".txt");//与上面的结果相同,将两个字符串组成一个路径
     
  TextBox1.Text = path;
       if (File.Exists(path))//检测文件是否存在
       {
            TextBox2.Text = File.ReadAllText(path);


       }

 protected void Button2_Click(object sender, EventArgs e)
    {
        string fname = DropDownList1.SelectedValue;
        string content = TextBox2.Text;
      string path = Path.Combine(Server.MapPath("txt"),fname+".txt");//与上面的结果相同,将两个字符串组成一个路径
      File.WriteAllText(path, content);
    }

protected void Button3_Click(object sender, EventArgs e)
    {
        string name = DropDownList1.SelectedValue;
        string path = Path.Combine(Server.MapPath("txt"), name + ".txt");
        if (File.Exists(path))
        {
            string[] content = File.ReadAllLines(path);
        TextBox _txt;
        foreach (string s in content)
        {
            _txt = new TextBox();
            _txt.Text = s;
            Panel1.Controls.Add(_txt);//Panel控件用于添加动态的文本框


        }
        }
    }
    }

上方name和path的格式必须一致否则会报错,不带扩展名path后面添加“.txt”不然 string name = Path.GetFile(s);


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值