简体   繁体   中英

Visual Studio 2017 C# - How do you import the contents of a local .txt list into a ComboBox's options?

All I need to do is import the contents of a .txt file (which is within the same folder that the VS solution file is in) into the options of a ComboBox on a form I'm making in VS. Everything that's come up in Google has not worked so far.

The list items in the .txt file are separated line-by-line, like this:

asdf
sadf
sadf
asdf
asdf

Not sure if I get your question correctly, but in the OnLoad form event try:

using System.IO;
comboBox.Items.AddRange(File.ReadAllLines(Path.Combine(Environment.CurrentDirectory, "myFile.txt")).Where(x => !string.IsNullOrWhiteSpace(x)).ToArray());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM