更新时间:2022-04-06 08:33:05
这将为您提供帮助.
Will this help you.
private void Form1_Load(object sender, EventArgs e)
{
string[] files = Directory.GetFiles("DirectoryPath");
for (int i = 0; i < files.Length; i++)
{
comboBox1.Items.Add(files[i]);
}
}
另外,如果只想显示文件名,则可以使用Path.GetFileName(files [i]);.或Path.GetFileNameWithoutExtension(files [i]);静态Path类中的方法.
注意:万一您不知道可以通过双击表单轻松地生成Form_Load事件处理程序.
如果您还想从子目录(文件夹中的文件夹)中获取文件,则可以执行以下操作:
Directory.GetFiles("DirectoryPath","*.*",SearchOption.AllDirectories);
"*.*"是搜索模式,因此,如果只需要文本文件,则可以将其更改为"* .txt"
also if you want to display just the file name you could use Path.GetFileName(files[i]); or Path.GetFileNameWithoutExtension(files[i]); methods from the static Path class.
Note: Just in case you don''t know you can easily generate a Form_Load event handler by double clicking on your form.
And if you want to get files from sub directories (folders within folders) as well you would do something like this:
Directory.GetFiles("DirectoryPath", "*.*", SearchOption.AllDirectories);
the "*.*" is the search pattern so if you only wanted text files you could change it to "*.txt"
您必须以Load()形式编写代码.
第一个
You have to write the code in your form Load().
First
string[] filePaths = Directory.GetFiles(@"c:\dir");
fot (int i = 0; i < filePaths.Length; ++i) {
string path = filePaths[i];
Console.WriteLine(System.IO.Path.GetFileName(path));
}
在此for循环中,检查每个文件扩展名以做
Inside this for loop check each files extension to do it
Use Path.GetExtension(string path)
如果是.txt,则将其添加到您的组合框中...
祝你好运.
if it is .txt then add to your combobox...
Good luck..
好像你想看看^ ]方法.它将目录中的所有文件提取到string[]
中.
干杯!—MRB
It looks as if you would want to check out theDirectory.GetFiles
[^] method. It fetches all the files of a directory into astring[]
.
Cheers!—MRB