【问题标题】:Get File Path From File in Auto-Saved ListBox从自动保存的列表框中的文件获取文件路径
【发布时间】:2017-03-23 20:36:49
【问题描述】:

我有一个保存的列表框。当有人退出程序时,它会将所有名称保存在列表框中,当他们进入时,会将它们全部添加回来。

问题是,当表单打开并且 listBox2 为空时,它很好并且可以执行它的功能。但是当程序在 Form_Load 上自动将文本框中的项目加载到 listBox2 时,单击 listBox2 时出现以下错误。

“System.ArgumentOutOfRangeException”类型的未处理异常 发生在 mscorlib.dll 中

附加信息:索引超出范围。必须是非负数 并且小于集合的大小。

在字符串fullFileName = selectedFiles[listBox2.SelectedIndex];

private void materialFlatButton3_Click_1(object sender, EventArgs e)
        {
            OpenFileDialog OpenFileDialog1 = new OpenFileDialog();
            OpenFileDialog1.Multiselect = true;
            OpenFileDialog1.Filter = "DLL Files|*.dll";
            OpenFileDialog1.Title = "Select a Dll File";
            if (OpenFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                // put the selected result in the global variable
                fullFileName = new List<String>(OpenFileDialog1.FileNames);


                foreach (string s in OpenFileDialog1.FileNames)
                {
                    listBox2.Items.Add(Path.GetFileName(s));
                    selectedFiles.Add(s);
                }

            }
        }

List<string> selectedFiles = new List<string>();
        private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (listBox2.SelectedIndex >= 0)
            {
                string fullFileName = selectedFiles[listBox2.SelectedIndex];
                textBox4.Text = fullFileName;
            }
            else
            {

            }




string path = @"C:\Save.txt";

       private void Form1_Load(object sender, EventArgs e)
                {
                    if (!File.Exists(path))
                    {

                        FileStream fs = File.Create(path);
                        fs.Close();

                    }
                    else
                    {
                        StreamReader sr = new StreamReader(path);
                        string line = string.Empty;
                        try
                        {
                            //Read the first line of text
                            line = sr.ReadLine();
                            //Continue to read until you reach end of file
                            while (line != null)
                            {
                                this.listBox2.Items.Add(line);
                                //Read the next line
                                line = sr.ReadLine();
                            }

                            //close the file
                            sr.Close();
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message.ToString());
                        }
                        finally
                        {
                            //close the file
                            sr.Close();
                        }




                            textBox3.Visible = false;
                            string text = File.ReadAllText(path, Encoding.UTF8);

                        }
                    }
                }

【问题讨论】:

  • 将 Form_Load 中的代码添加到问题中。
  • 我添加了 Form_Load 代码

标签: c# winforms listbox


【解决方案1】:

你的代码有一个很大的缺陷,你有一个带有文件名的ListBox,还有一个带有路径的List&lt;string&gt;,但是你只保存了ListBox上的数据,所以当你恢复的内容时ListBox 你的 List&lt;string&gt; 仍然是空的,这就是为什么它在 SelectedIndexChanged 上给你一个例外。

您还需要以某种方式存储路径,然后在加载时恢复它。最简单的解决方案可以是在文件上交错数据,从列表框中保存文件名,从列表中保存路径等等,直到您保存所有内容,然后当您读回它时,您使用列表框恢复一行名称和带有路径的列表的一行。

编辑:更好,而不是保存 ListBox 中的内容,而是保存 List&lt;string&gt; 中的内容,然后您可以这样做:

line = sr.ReadLine();
//Continue to read until you reach end of file
while (line != null)
{
    this.listBox2.Items.Add(Path.GetFileName(line));
    selectedFiles.Add(line);
    //Read the next line
    line = sr.ReadLine();
}

//close the file
sr.Close();

【讨论】:

  • 非常感谢您的修复。我现在明白它为什么这样做了!
  • 事情是这样的。重新加载时,可能会停止错误,但是当我重新加载时,不会在 textBox4 中显示路径。
  • 检查文件是否已正确保存和读取(检查 selectedFiles 和 listBox2 内容)
  • 在 FormClosing 上,它只保存 ListBox2 中的名称并写入文件。因此文本文件没有路径,只有文件名。有什么推荐的修复方法吗?
  • 你读过编辑吗?保存 List 中的内容,而不是 ListBox,然后使用我添加的代码。
猜你喜欢
  • 2010-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多