1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Drag Drop FROM VB.net TO Windows Explorer

Discussion in 'Visual Basic [VB]' started by ManzZup, Aug 31, 2010.

  1. ManzZup

    ManzZup New Member

    Joined:
    May 9, 2009
    Messages:
    278
    Likes Received:
    43
    Trophy Points:
    0
    Occupation:
    Production Manager:Software @ ZONTEK
    Location:
    Sri Lanka
    Home Page:
    I have searched long enough to release that there's no other SIMPLER way to archive the task of dragging out a virtual file ( a file that alreagy does not exist) Out of a VB.net program. This method is not 100% perfect but of course may be easier than calling the harsh shells.

    Background



    I was thinking to write this tut a long before but had some prob with time :). So for the start i was searching for method to DRAG -out something From a listbox/listview to outside of the form (of course dragging in is damn easy) but never found a good tutorial showing how. But then came to know about the doDragDrop() function which can drag and drop an EXISTING file (ex: if you have a list of file that's existing on the C:\ drive you can use this to drag the file out on to anywhere" but the problem occurred when this is a virtual file \ex: the file to extracted, downloaded

    But there's no returning value of doDragDrop so to i searched and found to ways:
    1. The hard way: this is for calling the native code and had to do some hard coding [The official way]
    2. The easy way: this is a method i saw propsed by someone (credits goes for his idea :D) then i coded it [The cheating way]

    The Code



    The logic:
    1. Add fileSystemWatcher to watch any creation of a file with unusual ext (like .abcdefghbbbbb :) )
    2. Then create a file(hidden,system attributed) with the ext with unusual ext at the form load.
    3. At the doDragDrop() function write the code to copy the already made file to the target directory. This fires the fileSystemWatcher
    4. Store the path taken by fileSystemWatcher and use it to do the custom action (of our own preferred way :D )
    Lets start:

    1) First we need to setup file system watchers and the form load

    Code:
    
    Public Sub setupFSMs()         'this part of the code credits goes to the one showed the logic :D
    
    	Dim Drives() As IO.DriveInfo = IO.DriveInfo.GetDrives 'This retrieves a complete list of the drives used in the pc.
    
    	For i As Integer = 0 To Drives.Length - 1      'Make a loop to add filesystemwatcher to EVERY drive and thier sub folders.
    
    		If Drives(i).DriveType = IO.DriveType.Fixed Then
    			If Drives(i).IsReady Then
    				Dim FSM As IO.FileSystemWatcher = New IO.FileSystemWatcher(Drives(i).RootDirectory.FullName, "*.antf2") 'antf2 is my ext use your own if you wish but careful to change it sith the rest of the code.
    
    				AddHandler FSM.Created, AddressOf OnChanged
    				FSM.IncludeSubdirectories = True
    				FSM.EnableRaisingEvents = True
    			End If
    		End If
    	Next
    End Sub
    
    Private Sub form_load(byval .........)
             setupFSMs
            'The below part too is CRITICAL. This creates the file that should be copied. You ca change tmp_donot_delete.antf2 to your own but PLEASE change the code suiting it.
    
             If Not IO.File.Exists(My.Application.Info.DirectoryPath & "\tmp_donot_delete.antf2") Then
                IO.File.Create(My.Application.Info.DirectoryPath & "\tmp_donot_delete.antf2")
                SetAttr(My.Application.Info.DirectoryPath & "\tmp_donot_delete.antf2", FileAttribute.Hidden + FileAttribute.System)
            End If
    End Sub
    
    Above initiate the fileSystemWatchers. Note the ext. i have used. its "antf2" which i felt like an uncommon one remeber to use your own one as i dont want ma prog go messing around

    2) Now the code to say What To Do when we find a drop of that file.

    Code:
    Sub OnChanged(ByVal source As Object, ByVal e As IO.FileSystemEventArgs)
    	' Specify what is done when a file is created.
    	' NOTE this runs on a seperate thread!
    
    	output = IO.Path.GetDirectoryName(e.FullPath)
    	My.Computer.FileSystem.DeleteFile(output.Trim(quote) & "\tmp_donot_delete.antf2")   'This is to prevent the file being there too much. This delete the file in the instance its created and stores the output path.
    
    	msgbox(output)
    	'Add your code here (you preferred action) mine was to extract the file to the output path.
    End Sub
    
    Add the actionyou need in 'Add your code here part PLEASE note that this action runs on a seperate thread so be creaful not to add actions with cross-thread references.

    3) Now to add the real part, where the file is dragged out Here i have used a lstView and i coded in the itemDrag. You can use events of your own in your own control but remeber to change the code.

    Code:
    If lstMain.SelectedItems.Count > 0 Then           
    	Dim fileas As String() = New [String](0) {}
    	fileas(0) = My.Application.Info.DirectoryPath & "\tmp_donot_delete.antf2"
    	dta = New DataObject(DataFormats.FileDrop, fileas)
    	dta.SetData(DataFormats.StringFormat, fileas)
    	DoDragDrop(dta, DragDropEffects.Copy)    'This copy the tmp_donot_delete.antf2 to the destination, get the destination to the output variable, delete the temp_donot....antf2, and finially run your code.
    End If
    
    [Credits for the idea and the FSMs goes to the one who proposed]

    Final Notes: This method is not 100% good, but easy to use. If you really need it in the professional way you'll have to extend your searches on shellExts. This doesnt work on network drives so be careful.
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,285
    Likes Received:
    364
    Trophy Points:
    83
  3. ManzZup

    ManzZup New Member

    Joined:
    May 9, 2009
    Messages:
    278
    Likes Received:
    43
    Trophy Points:
    0
    Occupation:
    Production Manager:Software @ ZONTEK
    Location:
    Sri Lanka
    Home Page:
    sorry,but do i have to nominate?
    if so how to do? (yah havent read the instructions well :) )
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,285
    Likes Received:
    364
    Trophy Points:
    83
    Just read it or else just post in the thread a link to the thread you want to nominate.
     
  5. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,285
    Likes Received:
    364
    Trophy Points:
    83

Share This Page