c# - 用文本文件中的值填充列表框

标签 c# file-io io listbox

我的代码有问题。我的代码必须从文本文件中读取值,它确实这样做了。但是,当我将文本文件的值放入我的列表框中时,结果如下所示:

commandlistbox

因此命令或值排在一行中。 我希望命令是这样的,我已经更改了图片以便您可以看到:

enter image description here

所以你看到了吗?我想要彼此下的命令。这是我读取文本文件的代码:

private void CommandFileSelectButton_Click(object sender, EventArgs e)
    {
        Stream mystream;
        OpenFileDialog commandFileDialog = new OpenFileDialog();


        if (commandFileDialog.ShowDialog() == DialogResult.OK)
        {
            if ((mystream = commandFileDialog.OpenFile())!= null)
            {
                string fileName = commandFileDialog.FileName;
                CommandListTextBox.Text = fileName;
                string fileText = File.ReadAllText(fileName);
                _commandList.Add(fileText);
                CommandListListBox.DataSource = _commandList;
            }

        }
    }

_commandList 是我的同事制作的本地函数。

这是 TextFile 的样子:

RUN 
RUNW
STOP
RUN
RUN
STOP

在此先感谢您的帮助。

最佳答案

CommandListListBox.DataSource = File.ReadAllLines(fileName);

关于c# - 用文本文件中的值填充列表框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19535033/

相关文章:

python - 创建一个空文件并在一行中关闭

c++ - FilePointer 陷入无限循环

java - 如何在 QtJambi 中获取标准输入、标准输出、标准错误文本流的 QIODevice 实例?

c# - 异常中断,*此处*除外(Visual Studio 2010 C#)

c# - 单例, 临界区

c# - WPF重构: Moving DataTemplate to another file

asp.net - 如何最好地生成 CSV(逗号分隔文本文件)以供使用 ASP.NET 下载?

c++ - 将文本从文件加载到二维数组中 (C++)

c# - 文件打开/保存对话框

c++ - fstream对象默认关联