【问题标题】:Drag&Drop - differentiate between file and folder拖放 - 区分文件和文件夹
【发布时间】:2012-11-10 19:35:03
【问题描述】:

当用户删除文件夹或文件时,我正在尝试区分,但出现错误。

更新

错误是:

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

这是潜艇:

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

【问题讨论】:

  • 什么“错误”?你得到一个异常或者它不识别目录(总是作为“文件”)?
  • 抱歉,感谢您的评论,我已经更新了我的问题

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


【解决方案1】:

你的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

【讨论】:

  • 非常感谢,您的第一条评论就足够了
猜你喜欢
  • 1970-01-01
  • 2013-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-05
  • 1970-01-01
  • 2014-09-20
  • 2020-02-10
相关资源
最近更新 更多