private void ToonInhoud()
{
string path = AppDomain.CurrentDomain.BaseDirectory;
string[] filePaths = Directory.GetFiles(path, "*.txt");
foreach (string file in filePaths)
{
cboLanden.Items.Add(System.IO.Path.GetFileNameWithoutExtension(file));
}
}
private void cboLanden_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
string path = AppDomain.CurrentDomain.BaseDirectory;
StreamReader oSR = new StreamReader(path);
String sLijn = oSR.ReadLine();
while (sLijn != null)
{
string shops = sLijn;
lstWinkels.Items.Add(shops);
sLijn = oSR.ReadLine();
}
oSR.Close();
}
我在第一个组合框中加载了3个TEXT文件,当选择了1个TEXT文件时,我想使用streamreader在列表框中显示内容!所以当你选择TEXT文件1时,我想要列表框中的TEXT文件1的内容,当我选择TEXT文件2时,我想要列表框中的TEXT文件2的内容,依此类推...... 如此绝望...... 提前谢谢!
答案 0 :(得分:0)
添加所选文件中的行:
void cboLanden_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
foreach(string fileName in e.AddedItems)
{
string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName);
foreach(string line in File.ReadLines(path))
lstWinkels.Items.Add(line);
}
// handle RemovedItems
}
如果您想获得所选文件的全部内容,请使用
string content = File.ReadAllText(path)