拖放 - 区分文件和文件夹

时间:2012-11-22 11:45:18

标签: vb.net visual-studio drag-and-drop

当用户删除文件夹或文件但我收到错误时,我正试图区别对待。

更新

错误是:

Conversion from string "C:\Users\Administrador\Desktop\W" to type 'Long' is not valid.

(注意路径不完整)

IDE突出显示此错误行:

If ((attributes And FileAttributes.Directory) = FileAttributes.Directory) Then

我不知道问题是在转换中还是什么,但如果我在问题错误行之前使用msgbox,我可以看到路径是如何正确的:

MessageBox.Show(Objetos(0))
If ((attributes And FileAttributes.Directory) = FileAttributes.Directory) Then

enter image description here

这是子:

Private Sub TextBox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles foldertextbox.DragDrop
    If e.Data.GetDataPresent(DataFormats.FileDrop) Then
        Dim Objetos As String() = e.Data.GetData(DataFormats.FileDrop)

        Dim attributes = Objetos(0)
        If ((attributes And FileAttributes.Directory) = FileAttributes.Directory) Then
            MsgBox("it's a dir")
        Else
            MsgBox("it's a file")
        End If

        foldertextbox.Text = Objetos(0)
        userSelectedFolderPath = Objetos(0)
        My.Settings.folderpath = Objetos(0)
        My.Settings.Save()
        playerargs = Nothing
        updatecheckboxes()
    End If
End Sub

1 个答案:

答案 0 :(得分:3)

你的attributes变量实际上包含文件/目录名称/路径,如果你想测试它是否是一个目录,你可以尝试:

System.IO.Directory.Exists(attributes)

像这样:

If System.IO.Directory.Exists(attributes) Then
    MsgBox("it's a dir")
ElseIf System.IO.File.Exists(attributes) Then
    MsgBox("it's a file")
End If

<强>更新

检查这个答案,有更好的检查,也许是你在找什么: .NET How to check if path is a file and not a directory?

Dim isDir As Boolean = (System.IO.File.GetAttributes(path) And 
        System.IO.FileAttributes.Directory) = FileAttributes.Directory