读取文本文件2
来源:互联网 发布:高中编程软件 编辑:程序博客网 时间:2024/06/05 07:33
利用 DropDownList1控件制作一个文件下拉列表
1、在后台添加一个DropDownList1
2、using System.IO;
<style type="text/css">
#Panel1input
{display:block; width:400px;}控制分行进行换行
</style>
input是通过在运行状态下点击网页查看源代码
<div id="Panel1"> <input name="ctl02" type="text"value="大飞多发点" /><input name="ctl03"type="text" value="更符合规范和看" /><inputname="ctl04" type="text" value="后天发货同样" /> </div>
<asp:DropDownList ID="DropDownList1" runat="server" >
</asp:DropDownList>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="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" onclick="Button2_Click" />
<asp:Button ID="Button3" runat="server" onclick="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);
- 读取文本文件2
- 保存文本文件, 读取文本文件
- 读取文本文件
- 读取文本文件
- 读取文本文件
- 读取文本文件
- 文本文件读取
- 逐行读取文本文件
- C#读取中文文本文件
- 如何读取、修改文本文件
- 如何读取、修改文本文件
- C#读取文本文件
- asp读取文本文件
- Flash读取外部文本文件
- vc读取文本文件
- qt4:读取文本文件
- 读取大文本文件
- 读取文本文件数据
- 有向图的十字链表存储表示 以及相关操作(包括增加弧、删除弧、删除顶点等)
- UVa:10994 Simple Addition
- PHP基础学习
- 11
- android 多点触碰实现图片的缩放
- 读取文本文件2
- 解释比特币原理
- 8个实用的netcat命令实例
- Javascript 函数 变量 注意
- 鲜为人知而又实用的 Linux 命令大全
- UVa 10047 The Monocycle
- 为什么硬链接不能指向目录
- ENGLISH资料收集(25)-经典电影告诉我们的
- Linux cron 任务 不执行/cron 任务详解