Stream read selected listbox item query

2019-09-03 08:07发布

问题:

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.

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.

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

My code

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

Thanks

回答1:

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

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

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

to this:

lstFiles.Items.Add(value);

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.

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;


标签: c# listbox