You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Visual Studio 2022中ListBox仅显示文件名时读取文件报错求助

解决ListBox仅显示文件名时读取文件找不到的问题

问题现象

使用Visual Studio 2022开发的小程序,将指定目录下的.txt文件加载到ListBox时仅显示无路径无扩展名的文件名,选中ListBox项读取内容到TextBox时触发System.IO.FileNotFoundException,错误提示找不到类似C:DistancebinDebugfriend的文件——原因是程序直接用ListBox中存储的纯文件名作为路径去读取,默认从当前运行目录查找,而非原文件所在目录,且缺少.txt扩展名。


解决方案

方法1:存储完整路径,自定义ListBox显示文本

通过ListBoxItem的Tag属性存储文件完整路径,同时设置Text属性显示纯文件名,既满足显示需求,又能准确获取文件路径。

修改加载文件的代码:

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles btnCarica.Click
    Dim dirpath As String = FolderBrowserDialog1.SelectedPath '选择目标目录
    '仅筛选目录下的.txt文件,避免加载无关文件
    Dim cerca() As String = Directory.GetFiles(dirpath, "*.txt")
    lsbNomi.Items.Clear() '清空ListBox原有内容
    For Each x As String In cerca
        Dim listItem As New ListBoxItem()
        listItem.Text = Path.GetFileNameWithoutExtension(x) '显示无路径无扩展名的文件名
        listItem.Tag = x '存储文件完整路径
        lsbNomi.Items.Add(listItem)
    Next        
End Sub

修改ListBox点击读取的代码:

Private Sub ListBox1_Click(sender As Object, e As EventArgs) Handles lsbNomi.Click
    If lsbNomi.SelectedItem Is Nothing Then
        MessageBox.Show("Selezionare un File esistente!", "ATTENZIONE!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        Exit Sub
    End If
    '从Tag中获取完整文件路径
    Dim fullFilePath As String = DirectCast(lsbNomi.SelectedItem, ListBoxItem).Tag.ToString()
    Dim leggi As StreamReader = File.OpenText(fullFilePath)
    txtDati.Text = "" '先清空TextBox原有内容
    While leggi.Peek <> -1
        txtDati.Text += leggi.ReadLine() + vbCrLf
    End While
    leggi.Close()
    '根据行数设置TextBox可用状态
    Dim lineCount As Integer = txtDati.Lines.Count
    txtDati.Enabled = lineCount > 28
    lsbNomi.Enabled = False
End Sub

方法2:保存目录路径,读取时拼接完整路径

将用户选择的目录路径存储为全局变量,读取时拼接纯文件名与.txt扩展名,生成完整文件路径。

  1. 定义全局变量存储目录路径:
Private selectedDirectory As String = String.Empty
  1. 修改加载文件的代码:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles btnCarica.Click
    selectedDirectory = FolderBrowserDialog1.SelectedPath '保存选择的目录路径
    Dim cerca() As String = Directory.GetFiles(selectedDirectory, "*.txt")
    lsbNomi.Items.Clear()
    For Each x As String In cerca
        lsbNomi.Items.Add(Path.GetFileNameWithoutExtension(x))
    Next        
End Sub
  1. 修改ListBox点击读取的代码:
Private Sub ListBox1_Click(sender As Object, e As EventArgs) Handles lsbNomi.Click
    If String.IsNullOrEmpty(lsbNomi.Text) Then
        MessageBox.Show("Selezionare un File esistente!", "ATTENZIONE!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        Exit Sub
    End If
    '用Path.Combine拼接完整路径,自动处理路径分隔符
    Dim fullFilePath As String = Path.Combine(selectedDirectory, lsbNomi.SelectedItem.ToString() & ".txt")
    '提前判断文件是否存在,避免异常
    If Not File.Exists(fullFilePath) Then
        MessageBox.Show("文件不存在!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Exit Sub
    End If
    Dim leggi As StreamReader = File.OpenText(fullFilePath)
    txtDati.Text = ""
    While leggi.Peek <> -1
        txtDati.Text += leggi.ReadLine() + vbCrLf
    End While
    leggi.Close()
    Dim lineCount As Integer = txtDati.Lines.Count
    txtDati.Enabled = lineCount > 28
    lsbNomi.Enabled = False
End Sub

关键注意点

  • 加载文件时筛选.txt类型:避免加载非目标文件,减少后续读取异常
  • 使用Path.Combine拼接路径:比直接字符串拼接更安全,自动适配不同系统的路径分隔符
  • 读取前判断文件状态:提前检查文件是否存在,提升程序稳定性

内容的提问来源于stack exchange,提问作者Maurizio Vignazzi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 13:35:01