从列表框中删除并选择一个文件

时间:2018-02-14 23:10:46

标签: vb.net listbox

经过多次试验和错误后,我找到了需要通过的撞墙,我需要你的帮助,但首先让我告诉你到目前为止我所拥有的......

LB_FolderPathListBox,其中包含我的所有工作路径。

LB_Dwglst ListBox包含我工作文件夹中的所有文件(LB_FolderPath)。

当我从LB_Dwglst中选择文件或尝试使用Bt_RmvPrj按钮删除路径时,出现以下错误:

  

“System.UnauthorizedAccessException”类型的未处理异常   发生在mscorlib.dll中附加信息:访问路径   'u:\ 123409 \ mech'被拒绝。

文件夹和文件不是只读的,它看起来像(我认为)我需要检查文件的现有...你能告诉我该怎么做吗?

这是我的代码......

Dim folderName As String

Private Sub Bt_RmvPrj_Click(sender As Object, e As EventArgs) Handles Bt_RmvPrj.Click
    ' 
    ' removing items.
    For i As Integer = LB_FolderPath.Items.Count - 1 To 0 Step -1
        If LB_FolderPath.GetSelected(i) Then
            LB_FolderPath.Items.RemoveAt(i)
        End If
    Next i

End Sub

Private Sub LB_Dwglst_SelectedIndexChanged(sender As Object, e As EventArgs) Handles LB_Dwglst.SelectedIndexChanged
    Dim Path = LB_FolderPath.SelectedItem.ToString
    Dim strFileName As String = LB_Dwglst.SelectedIndex
    Dim selectedItem = LB_Dwglst.SelectedItem
    If (File.Exists(strFileName)) >= 0 Then

      ' ***the error in this line below...***
        Dim fs As FileStream = New FileStream(Path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)

        ' An unhandled exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll
        ' Additional Information: Access to the path 'U:\100000\Telecom' is denied.

more LONG code below to preview the cadd file.


Private Sub LB_FolderPath_SelectedIndexChanged(sender As Object, e As EventArgs) Handles LB_FolderPath.SelectedIndexChanged
    LB_Dwglst.Items.Clear()
    ' Obtain the file path from the list box selection.
    Dim filePath = LB_FolderPath.SelectedItem.ToString
    Dim fileNames As String() = System.IO.Directory.GetFiles(filePath, "*.dwg", SearchOption.TopDirectoryOnly)
    For Each fileName As String In fileNames
         LB_Dwglst.Items.Add(Path.GetFileName(fileName))
    Next
End Sub

My project

0 个答案:

没有答案