【问题标题】:File not found error from the listbox C# win forms来自列表框 C# winforms 的文件未找到错误
【发布时间】:2012-05-10 04:45:30
【问题描述】:

我有一个列表框,它有一些从目录文件夹中加载的文件。

将文件加载到 listBox1 的代码:

private void Form1_Load(object sender, EventArgs e)
        {
            PopulateListBox(listbox1, @"C:\TestLoadFiles", "*.rtld");

        }

private void PopulateListBox(ListBox lsb, string Folder, string FileType)
        {
            DirectoryInfo dinfo = new DirectoryInfo(Folder);
            FileInfo[] Files = dinfo.GetFiles(FileType);
            foreach (FileInfo file in Files)
            {
                lsb.Items.Add(file.Name);
            }
        }

我想读取并显示表单中标签的属性值。 listBox1中加载的文件,代码如下:

private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
    string path = (string)listBox1.SelectedItem;
    DisplayFile(path);
} 
private void DisplayFile(string path)
{
    string xmldoc = File.ReadAllText(path);

    using (XmlReader reader = XmlReader.Create(xmldoc))
    {   

        while (reader.MoveToNextAttribute())
        {
          switch (reader.Name)
          {
            case "description":
              if (!string.IsNullOrEmpty(reader.Value))
                label5.Text = reader.Value; // your label name
              break;
            case "sourceId":
              if (!string.IsNullOrEmpty(reader.Value))
                label6.Text = reader.Value; // your label name
              break;
            // ... continue for each label
           }
        }
    }
} 

Problem:当我在加载表单后单击listBox1中的文件时,文件从文件夹加载到列表框中,但抛出错误File not found in the directory

我该如何解决这个问题???

【问题讨论】:

    标签: c# winforms visual-studio-2010


    【解决方案1】:

    那是因为您要添加 File.Name 而不是您应该在列表框中添加 File.FullName

    lsb.Items.Add(file.FullName);
    

    所以你的方法 PopulateListBox 应该变成:

    private void PopulateListBox(ListBox lsb, string Folder, string FileType)
            {
                DirectoryInfo dinfo = new DirectoryInfo(Folder);
                FileInfo[] Files = dinfo.GetFiles(FileType);
                foreach (FileInfo file in Files)
                {
                    lsb.Items.Add(file.FullName);
                }
            }
    

    编辑: 看起来您只想显示文件名,而不是完整路径。您可以遵循以下方法。在 PopulateListBox 中,添加 file 而不是 file.FullName 所以该行应该是

    foreach (FileInfo file in Files)
                {
                    lsb.Items.Add(file);
                }
    

    然后在 SelectedIndexChanged 事件中执行以下操作:

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                FileInfo file = (FileInfo)listbox1.SelectedItem;
                DisplayFile(file.FullName);
             }
    

    这应该会为您提供全名(带路径的文件名)并将解决您的 File Not Found 异常

    【讨论】:

    • @KarthikRANGARAJ,我没听懂你,你想只显示名字然后得到全名来显示文件吗?
    • 在listBox1中,我必须只显示文件夹中的file names,当我点击它时,它应该将相应文件的属性值读入标签中。
    • 非常感谢您的帮助,我这里还有一个问题,我在illegal characters in pathusing (XmlReader reader = XmlReader.Create(xmldoc)) 处遇到错误,您能帮我解决这个问题吗?
    • @KarthikRANGARAJ,XML 可能有问题,您可以在 XML 中发布一个新问题,或者尝试看看是否可以调试它
    【解决方案2】:

    您面临的问题是,在列表框中您只指定了文件名,而不是整个文件路径和名称,所以当它查找文件时,您找不到它。

    来自FileInfo.Name Property

    Gets the name of the file.
    

    File.ReadAllText Method (String)path 作为参数。

    【讨论】:

      【解决方案3】:

      您没有指定完整路径。

      试试这样的:

       DisplayFile(@"C:\TestLoadFiles\" + path)
      

      【讨论】:

      • 我使用了你的方法,但是在illegal characters in pathusing (XmlReader reader = XmlReader.Create(xmldoc))@illegal characters in path 这一行中出现错误
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-17
      • 1970-01-01
      • 2018-05-15
      • 2018-11-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多