Results 1 to 8 of 8

Thread: [RESOLVED] Drag Drop Get Filename & Restrict Files

  1. #1

    Thread Starter
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    Resolved [RESOLVED] Drag Drop Get Filename & Restrict Files

    Ola,

    1) I'm dragging a file (.txt) into a rtb which works fine, but how do I get the filename? (messagebox whatever)
    2) How do I restrict files to drop? (like a .exe)

    vb.net Code:
    1. If e.Data.GetDataPresent(DataFormats.FileDrop) Then
    2. Dim Files() As String
    3. Files = e.Data.GetData(DataFormats.FileDrop)
    4. Dim sReader As New StreamReader(Files(0))

    Thanks in advance.


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  2. #2

    Thread Starter
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    Re: Drag Drop Get Filename & Restrict Files

    I've found the answer to my 1st question, now only to restrict certain files.

    for the people who want to know how:

    Code:
    Imports System.IO
    
    If e.Data.GetDataPresent(DataFormats.FileDrop) Then
    Dim Files() As String
    Files = e.Data.GetData(DataFormats.FileDrop)
    Dim sReader As New StreamReader(Files(0))
    
    Dim file_name as String = Path.Getfilename(Files(0))
    msgbox(file_name)
    Last edited by Radjesh Klauke; Jan 28th, 2011 at 06:27 AM.


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  3. #3
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Drag Drop Get Filename & Restrict Files

    Quote Originally Posted by Radjesh Klauke View Post
    I've almost found the answer fo my 1st question, but the result is also with the path of the file. I only want the filename:
    Code:
    MsgBox(Files(0))
    Result: C:\Users\Radjesh\Desktop\test.txt I only want test.txt.
    have a look at....
    Path.GetFileName Method and Path.GetExtension Method

  4. #4

    Thread Starter
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    Re: Drag Drop Get Filename & Restrict Files

    Yeah, stupid of me that I didn't think of that, but that's already fixed m8. Thanks for thinking with me.

    Ah... The Path.GetExtension Method is something that I have to read. Thanks so far.


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  5. #5

    Thread Starter
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    Re: Drag Drop Get Filename & Restrict Files

    The Path.GetExtension Method did it. Thanks for the great help. +1

    For the readers:
    Code:
    Dim Files() As String
    Files = e.Data.GetData(DataFormats.FileDrop)
    Dim sReader As New StreamReader(Files(0))
    
    '   get the filename from the file without the path
    Dim file_name As String = Path.GetFileName(Files(0))
    
    '   check the extension of the file
    If Path.GetExtension(Files(0)) = ".txt" Or _
    Path.GetExtension(Files(0)) = ".blabla" Then
    ' do your thing
    Else
    ' show warning
    End if


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  6. #6
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Drag Drop Get Filename & Restrict Files

    Quote Originally Posted by Radjesh Klauke View Post
    The Path.GetExtension Method did it. Thanks for the great help. +1
    Might want to change that to lower case....
    If Path.GetExtension(Files(0)).ToLower = ".txt"

  7. #7

    Thread Starter
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    Re: Drag Drop Get Filename & Restrict Files

    And another great tip. Thanks.


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  8. #8
    New Member
    Join Date
    Jan 2016
    Posts
    7

    Re: [RESOLVED] Drag Drop Get Filename & Restrict Files

    HOW TO RESTRICT FOLDERS/DIRECTORIES:

    Just in case anyone comes looking for it, I did and this is what I was looking for. It's an elegant solution.

    Here is the link to the thread.

    This might help someone:

    Code:
        'Allow/deny entering of supplied images:
        Private Sub TestDrop_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TestDrop.DragEnter
    
            'Allow entering of file:
            If (e.AllowedEffect And DragDropEffects.Copy) = DragDropEffects.Copy AndAlso
           e.Data.GetDataPresent("FileDrop", True) Then
    
                'Get the file path of entered file for checking:
                Dim data As Object = e.Data.GetData("FileDrop", True)
                Dim filePaths As String() = DirectCast(data, String())
    
                'Check to see if it is a folder (isDir is false by default) - Note, "foldername.xls" would otherwise be passed:
                Dim isDir As Boolean = (System.IO.File.GetAttributes(filePaths(0)) And System.IO.FileAttributes.Directory) = FileAttributes.Directory
    
                If isDir = True Then
                    e.Effect = DragDropEffects.None
                    Exit Sub
                End If
    
                'Check that the file matches required length/number(1) and type (.xls):
                Dim fileExtension As String = Path.GetExtension(filePaths(0))
    
                If Not filePaths.Length = 1 Or Not fileExtension.ToLower = ".xls" Then
                    e.Effect = DragDropEffects.None
                Else
                    e.Effect = DragDropEffects.Copy
                End If
    
            End If
    
        End Sub
    
        Private Sub TestDrop_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TestDrop.DragDrop
    
            'Get the file path of dropped file:
            Dim data As Object = e.Data.GetData("FileDrop", True)
            Dim filePaths As String() = DirectCast(data, String())
    
        End Sub
    These threads helped me:
    - Restricting folders/directories
    - Restricting files
    Last edited by Kevin_Co; Jan 17th, 2016 at 09:26 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width