menu

Monday, August 30, 2010

Drag out FROM VB.net form TO windows explorer/dekstop/outside


hii everyone,

i was thinking to write this tut a long before but had some prob with time Smile.
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 tut showing how. But then came to knwo about the dodradrop() func which can dragdrop 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 whn 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 Big Grin) then i coded it [The cheating way]

###############################################
################THE CODE#########################

The logic:
1) Add fileSystemWatchers to watch any creation of a file with unusual ext (like .abcdefghbbbbb Smile )

2) Then create a file(hidden,system attributed) with the ext with unusual ext at the form load.

3) At the doDragDrop() func write the code to copy the already made file to the target directory. This fires the fileSystemWatcher

4) Store the path taken by fileSystemWather and use it to do the custom action (of our own preferred way Big Grin )

Lets start:
1) First we need to setup filesystem wacthers 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 create 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
    End Sub

[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.


Cyah All!

No comments:

Post a Comment