2013-05-08 56 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