2013-05-08 48 views
0

我有一个带有两个文本框的程序。一个是用于文件夹路径(目录)的另一个文件路径。现在,我的拖放功能正确地只允许一个文本框中的文件路径和另一个文件夹路径(目录)中的文件路径。在Visual Basic中拖放和验证(VS 2012 V11)

但是,我不确定我的验证方法是否正确。 这里是代码的两个部分验证,如果文件或目录:

If e.Data.GetDataPresent(DataFormats.FileDrop) Then 

      Dim filePathDragDrop() As String = CType(e.Data.GetData(DataFormats.FileDrop), String()) 

      For Each pathTemp In filePathDragDrop 

       ' This checks if it is a file 
       If File.GetAttributes(pathTemp) = FileAttributes.Archive Then 
        TextBoxCopyFrom.Text = pathTemp 
       End If 

      Next 

     End If 

以及检查目录中的一个类似的设置,如上面只有这是不同

' Checks for directory 
If File.GetAttributes(pathTemp) = FileAttributes.Directory Then 
        TextBoxCopyTo.Text = pathTemp 
       End If 

这是正确的检查被删除的文件是否确实是一个文件, 以及被删除的文件夹(目录)确实是一个文件夹(目录)?

是否有任何可能被丢弃的项目会被错误验证?

我假设“FileAttributes.Archive”的归档部分属于文件数据。

回答

0

不,这是不正确的。存档属性不必打开。它将在用户使用备份程序时关闭,并且该文件自备份以来未更改。实际上并不确定备份程序现在是否仍然如此,但是你不能做出任何假设。

目录测试不正确。你需要像这样测试:

If (File.GetAttributes(pathTemp) And FileAttributes.Directory) = FileAttributes.Directory Then 
    '' It's a directory 
Else 
    '' It's a file 
End If 
+0

好的,那有效!但是,有没有测试文件的东西是这样的:“如果(File.GetAttributes(pathTemp)和FileAttributes.Directory)= FileAttributes.File然后'它是一个文件'我知道没有”.file“但有东西测试文件(除了反向测试,“不是目录然后文件”)。 – user2348797 2013-05-08 23:16:16

+0

这没有任何意义。 – 2013-05-08 23:31:20

+0

我可能会假设:“If(File.GetAttributes(pathTemp)and FileAttributes.Directory)= FileAttributes.Directory”正在检查是否被拖放的项目是一个目录。如果“FileAttributes.Directory)= FileAttributes.Directory为true”,则被拖放的项目是一个目录。 “else”语句表示,如果被拖放的项目不是目录,则它必须是文件(例如:.txt,.exe,.oggs,.png,.jpg等)。我问的是,有没有办法查看拖放的内容是.txt,.ogg,.png等? – user2348797 2013-05-09 02:47:29