流读取选定的列表框项目查询 [英] Stream read selected listbox item query

查看:55
本文介绍了流读取选定的列表框项目查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an app that reads a directory, gets a list of the files (segy) and populates a listbox on the left side of the app with the filenames. Upon clicking an item in the listox I'd like the rich text box on the right to display the content of the file.

如果我使用openfiledialog选择目录中的文件之一,则我的应用程序正在运行,我在尝试让流阅读器读取我单击的所选文件时遇到问题.

I have the app working if I use the openfiledialog to select one of the files in the directory, I'm having issues trying to get the stream reader to read the selected file I've clicked.

下面的简单openfile对话代码.

The working simple openfiledialog code below.

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);

我的代码

private void txtUpdate(string value)
    {
        lstFiles.Items.Add(value + Environment.NewLine);
        lstFiles.TopIndex = lstFiles.Items.Count - 1;
        lstFiles.Update();
    }

    private void btnFolder_Click(object sender, EventArgs e)
    {
        txtPath.Text = "";
        lstFiles.Items.Clear();
        rtbHeader.Clear();
        DialogResult result = folderBrowserDialog1.ShowDialog();

        if (result == DialogResult.OK)
        {
            txtPath.Text = folderBrowserDialog1.SelectedPath;
        }
    }

    private void btnFiles_Click(object sender, EventArgs e)
    {
        lstFiles.Items.Clear();
        string path = txtPath.Text;
        List<string> files = new List<string>(Directory.EnumerateFiles(txtPath.Text, "*.sgy", SearchOption.AllDirectories).Select(Path.GetFileName).OrderBy(x => x));
        if (files == null || files.All(x => string.IsNullOrWhiteSpace(x)))
        {
            MessageBox.Show("There are no files with extension" + " sgy", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            return;
        }

        foreach (string file in files)
        {
            this.Invoke(new Action(() => txtUpdate(file)));
        }   
    }

    private void lstFiles_MouseClick(object sender, MouseEventArgs e)
    {
        rtbHeader.Clear();
        String item = (Convert.ToString(lstFiles.SelectedItem));
        //MessageBox.Show(item);
        StreamReader readFile = new StreamReader(item, 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;
    }
}

此位路径异常中出现非法字符.

Im getting an Illegal characters in path exception on this bit.

StreamReader readFile = new StreamReader(item, ebcdic);

谢谢

推荐答案

该代码示例确实应该更简单.简单得多.并且问题描述更加具体.更具体.请参见 https://stackoverflow.com/help/mcve

The code example really should be simpler. Much simpler. And the problem description more specific. Much more specific. See https://stackoverflow.com/help/mcve and https://stackoverflow.com/help/how-to-ask

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

That said, I believe that if you change this statement in the txtUpdate() method:

lstFiles.Items.Add(value + Environment.NewLine);

对此:

lstFiles.Items.Add(value);

它将起作用.该异常很可能是由于字符串中包含换行符而引起的.不仅使文件名不是您想要的文件名,而且在Windows路径中也不是有效字符.

It will work. The exception is most likely caused by the fact that you have newline characters in your strings. Not only does that make the filename not the one you want, it's not a valid character in Windows paths.

还请注意,您添加的ListBox中的项目已经是string.您不需要在它们上调用Convert.ToString().您可以将它们投射回string:

Also note that the items in the ListBox you've added are already strings. You don't need to call Convert.ToString() on them. You can just cast them back to a string:

String item = (string)lstFiles.SelectedItem;

这篇关于流读取选定的列表框项目查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆