|
-
Jan 28th, 2011, 04:15 AM
#1
Thread Starter
PowerPoster
[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:
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
Dim Files() As String
Files = e.Data.GetData(DataFormats.FileDrop)
Dim sReader As New StreamReader(Files(0))
Thanks in advance.
-
Jan 28th, 2011, 05:08 AM
#2
Thread Starter
PowerPoster
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.
-
Jan 28th, 2011, 06:26 AM
#3
Re: Drag Drop Get Filename & Restrict Files
 Originally Posted by Radjesh Klauke
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:
Result: C:\Users\Radjesh\Desktop\test.txt I only want test.txt.
have a look at....
Path.GetFileName Method and Path.GetExtension Method
-
Jan 28th, 2011, 06:29 AM
#4
Thread Starter
PowerPoster
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.
-
Jan 28th, 2011, 06:48 AM
#5
Thread Starter
PowerPoster
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
-
Jan 28th, 2011, 06:54 AM
#6
Re: Drag Drop Get Filename & Restrict Files
 Originally Posted by Radjesh Klauke
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"
-
Jan 28th, 2011, 06:57 AM
#7
Thread Starter
PowerPoster
Re: Drag Drop Get Filename & Restrict Files
And another great tip. Thanks.
-
Jan 17th, 2016, 09:23 AM
#8
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|