获取单击按钮时显示的文本文件列表 [英] Getting a list of text files to show when clicked on a button
问题描述
我希望当我点击一个名为Dutch的按钮时,它会加载到指定路径中的所有文件中,并在下拉列表中显示它们,您可以在其中选择其中一个文件,然后将其加载到固定文本框。
问题是,当我尝试这个时,它根本没有显示下拉列表中的文件名。
我用的是我从网站上获得的自定义控件:使用自定义控件的ButtonDropDown(ButtonDropDownMenu控件) [ ^ ]
但我不知道如何实施这些文本文件的自动加载。
所以我需要的是一种在下拉菜单中自动加载文件名的方法,当我点击它时,它会将其加载到文本框中。
这是我到目前为止所得到的。
I would like that when I click on a button called "Dutch" that it loads in all the files in a specified path and show them in a drop down list where you can chose 1 of those files that it then loads into a fixed textbox.
The problem is that when I try this, it doesn''t show me the file names in the drop down list at all.
I do use a custom control I got from the website : ButtonDropDown using Custom Control (ButtonDropDownMenu Control)[^]
But I don''t know how to implement the automatic loading of those text files.
So what I need is a way to automaticly load the file-names in the drop down menu and when I click on one it loads it into a text box.
This is what I got so far.
private void Dutch_Click(object sender, EventArgs e)
{
string[] dirs = Directory.GetFileSystemEntries(@"C:\Users\a961835\Programming","*.txt");
foreach (string dir in dirs)
{
textBox1.Text = dir;
}
//string s = File.ReadAllText(@"C:\Users\a961835\Programming");
//textBox1.Text = s;
}
推荐答案
现在试试这段代码,
now try this code,
string[] Filelistarray = Directory.GetFiles(@"D:\", "*.txt");
DataTable dt = new DataTable();
dt.Columns.Add("FileName");
foreach (string FileName in Filelistarray)
{
DataRow dr = dt.NewRow();
string filenameWithoutPath = Path.GetFileName(FileName);
dr["FileName"] = filenameWithoutPath;
dt.Rows.Add(dr);
}
DropDownList1.DataSource = dt;
DropDownList1.DataBind();
问候,
Prakash.T
regards,
Prakash.T
最终我使用了SoMad中的代码,但稍微调整了一下;)仍然感谢man:D
Eventually I used the code from SoMad but adjusted it a little bit ;) Still loads of thanks man :D
private void Dutch_Click(object sender, EventArgs e)
{
string[] Filelistarray = Directory.GetFiles(@"C:\Users\a961835\Programming", "*.txt");
DataTable dt = new DataTable();
dt.Columns.Add("FileName");
foreach (string FileName in Filelistarray)
{
DataRow dr = dt.NewRow();
string filenameWithoutPath = Path.GetFileName(FileName);
dr["FileName"] = filenameWithoutPath;
dt.Rows.Add(dr);
}
DropDownList.DataSource = dt;
DropDownList.DisplayMember = "FileName";
}
hi,
试试这个..
添加命名空间
使用System.IO;
try this..
add namespace
using System.IO;
string[] Filelistarray = Directory.GetFiles(@"D:\", "*.txt");
foreach (string FileName in Filelistarray)
{
textBox1.Text += FileName;
}
这篇关于获取单击按钮时显示的文本文件列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!