C#init combobox'带文件名 [英] C# init combobox 'with files name
本文介绍了C#init combobox'带文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以使用计算机上所有文件的名称启动组合框吗?
或者至少文件名位于项目文件夹中?
Can i boot combobox with the names of all the files on my computer?
Or at least the names of the files are located in the project folder?
推荐答案
http://stackoverflow.com/ questions / 6817639 / get-filenames-without-path-of-a-specific-directory [ ^ ]
是:最简单的方法是处理Form.Shown事件和添加:
Yes: the simplest way is to handle the Form.Shown event and add this:
string[] files = Directory.GetFiles(@"C:\MyProjects\", "*.*", SearchOption.AllDirectories);
myComboBox.Items.AddRange(files);
这会添加整个路径,因此如果您只想添加文件名,则必须使用一点Linq方法:
That adds the entire path, so if you want to add just the files names you will have to use a little Linq method:
string[] files = Directory.GetFiles(@"C:\MyProjects\", "*.*", SearchOption.AllDirectories);
var namesOnly = files.Select(f => Path.GetFileNameWithoutExtension(f)).ToArray();
myComboBox.Items.AddRange(namesOnly);
这篇关于C#init combobox'带文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文