Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 流式读取所选列表框项查询_C#_Listbox - Fatal编程技术网

C# 流式读取所选列表框项查询

C# 流式读取所选列表框项查询,c#,listbox,C#,Listbox,我有一个应用程序,它读取目录,获取文件列表,并用文件名填充应用程序左侧的列表框。单击列表框中的一项后,我希望右侧的富文本框显示文件的内容 如果我使用openfiledialog选择目录中的一个文件,那么我在尝试让流阅读器读取我单击的选定文件时遇到问题 下面是简单的openfiledialog代码 openFileDialog1.Filter = "All Files|*.*"; openFileDialog1.Title = "Open SEG-Y Files"; DialogResult re

我有一个应用程序,它读取目录,获取文件列表,并用文件名填充应用程序左侧的列表框。单击列表框中的一项后,我希望右侧的富文本框显示文件的内容

如果我使用openfiledialog选择目录中的一个文件,那么我在尝试让流阅读器读取我单击的选定文件时遇到问题

下面是简单的openfiledialog代码

openFileDialog1.Filter = "All Files|*.*";
openFileDialog1.Title = "Open SEG-Y Files";
DialogResult result = openFileDialog1.ShowDialog();
StreamReader readFile = new StreamReader(openFileDialog1.FileName, ebcdic);
readFile.BaseStream.Seek(0, SeekOrigin.Begin);
readFile.Read(data, 0, 3200);
string stringData = "";
for (int i = 0; i < data.Length; i++)
{
    if ((i % 80) == 0 && stringData != "")
        stringData += Environment.NewLine;
        stringData += data[i].ToString();
}
rtbHeader.Text = stringData;
rtb.AppendText(value);
rtb.AppendText(System.Environment.NewLine);

谢谢

代码示例应该更简单。简单得多。并且问题描述更加具体。具体得多。看到和

也就是说,我相信如果您在txtUpdate方法中更改此语句:

为此:

lstFiles.Items.Add(value);
它会起作用的。异常很可能是由于字符串中有换行符。这不仅使文件名不是您想要的文件名,而且在Windows路径中它不是有效字符

还要注意,您添加的列表框中的项目已经是字符串。您不需要对它们调用Convert.ToString。您可以将它们重新转换为字符串:


谢谢Peter,尝试了您的示例,但出现异常,表示无法在“我的项目调试”文件夹中找到该文件。我将示例更改为String item=stringtxtPath.Text+lstFiles.SelectedItem;这给了我正确的路径,但是路径和文件之间没有分隔符,例如FilePathFile1.sgy,所以我得到了一个文件未找到异常。Ok got it String item=stringtxtPath.Text+@\+lstFiles.SelectedItem;感谢您的帮助:-
StreamReader readFile = new StreamReader(item, ebcdic);
lstFiles.Items.Add(value + Environment.NewLine);
lstFiles.Items.Add(value);
String item = (string)lstFiles.SelectedItem;