2015-06-22 56 views
1

從Windows資源管理器瀏覽的Ftp文件夾中拖動文件時,可以使用下一個代碼讀取文件名。 但是有沒有辦法檢索完整的Ftp路徑?通過拖放檢索FTP文件的完整路徑?

Private Sub DataGridView1_DragDrop(sender As Object, e As System.Windows.Forms.DragEventArgs) Handles DataGridView1.DragDrop 
    Dim filename As String = "" 
    If e.Data.GetDataPresent("UniformResourceLocator") Then 
     Dim ioStream As System.IO.Stream = DirectCast(e.Data.GetData("FileGroupDescriptor"), System.IO.Stream) 
     Dim contents As Byte() = New [Byte](511) {} 
     ioStream.Read(contents, 0, 512) 
     ioStream.Close() 
     Dim sb As New System.Text.StringBuilder() 
     Dim i As Integer = 76 
     While contents(i) <> 0 
      sb.Append(CChar(ChrW(contents(i)))) 
      i += 1 
     End While 

     filename = sb.ToString() 
    End If 

End Sub 

回答

2

如果數據下降了包含UniformResourceLocator格式,你可以從得到整個URL,例如:

Private Sub Form1_DragDrop(sender As Object, e As System.Windows.Forms.DragEventArgs) Handles Me.DragDrop 

     If e.Data.GetDataPresent("UniformResourceLocator") Then 
     Dim URL As String = New IO.StreamReader(CType(e.Data.GetData("UniformResourceLocator"), IO.MemoryStream)).ReadToEnd 
     End If 

    End Sub 

它首先檢查,看是否有UniformResourceLocator格式存在,如果是這樣,從e(拖放參數)獲取數據,將其轉換爲MemoryStream,並將其傳遞給新的StreamReader(以便讀取),然後執行.ReadToEnd()以獲取整個字符串。

相關問題