USB's Data Copier!!!

asadullahpk

New member
Joined
Jan 17, 2008
Messages
3
Programming Experience
Beginner
Hi!
I am new to VB.Net. I want to develop an application which may copy all the data or some specific data from the USB Drive as it is plugged into the system. All this action should take place invisibly. Can anyone please help me starting the project and some details.
Thanks.
Muhammad Asad Ullah Bhatti,
GC University, Lahore,
Pakistan.
 
Well for starters, you can loop through the drives on the system and detect if it's a removable drive:
VB.NET:
For Each di As IO.DriveInfo In IO.DriveInfo.GetDrives
  If di.DriveType = IO.DriveType.Removable Then
    Console.WriteLine(di.Name)
  End If
Next di
So now you'd have a removable drive (I don't know the code right off hand, but you'll want to make sure it's a flash drive and not an external HDD, unless it doesn't matter)

But then you would use the System.IO.File and System.IO.Directory to do any coping and directory creating

I have code somewhere (originally made in VS 2003) that would automatically detect when a new drive becomes available on the system (a flash drive in my case) I can dig it up if you'd like to see it
 
Thanks!!!

Thank very much. Now I've started this task. Please tell me how to explore the availablity of such easy and helpful features in .Net.
Regards.
 
Here is a little taste of Device Management programming, works for me with different types of USB drives, this code is added to a form with a Listbox control on it, you should get the idea how to work with drive letter and figuring out drive type:
VB.NET:
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
    If m.Msg = WM_DEVICECHANGE Then
        Select Case m.WParam.ToInt32
            Case DBT_DEVICEARRIVAL
                'some device added
                If IsVolume(m.LParam) Then
                    For Each drive As Char In getDriveLetters(m.LParam)
                        Dim d As New IO.DriveInfo(drive)
                        Me.ListBox1.Items.Add(String.Format("drive {0} added, type {1}", drive, d.DriveType))
                    Next
                End If
        End Select
    End If
    MyBase.WndProc(m)
End Sub

'windows message
Public Const WM_DEVICECHANGE As Int32 = &H219
'wparams
Public Const DBT_DEVICEARRIVAL As Int32 = &H8000
'notification filter types
Public Const DBT_DEVTYP_VOLUME As Int32 = &H2

'checks if its a DEV_BROADCAST_VOLUME
Public Shared Function IsVolume(ByVal lParam As IntPtr) As Boolean
    Return (GetDeviceType(lParam) = DBT_DEVTYP_VOLUME)
End Function

'gets device type from a DEV_BROADCAST_HDR
Public Shared Function GetDeviceType(ByVal lparam As IntPtr) As Integer
    Return Runtime.InteropServices.Marshal.ReadInt32(lparam, 4)
End Function

'gets array of drive letters from a DEV_BROADCAST_VOLUME
Public Shared Function getDriveLetters(ByVal lparam As IntPtr) As Char()
    Dim mask As Integer = Runtime.InteropServices.Marshal.ReadInt32(lparam, 12)
    Dim drives As New List(Of Char)
    Dim driveletters As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    Dim b As New BitArray(New Integer() {mask})
    For x As Integer = 0 To driveletters.Length - 1
        If b(x) Then drives.Add(driveletters(x))
    Next
    Return drives.ToArray
End Function
It can also be achieved with WMI; a ManagementEventWatcher with this query: "SELECT * FROM __InstanceCreationEvent WITHIN 3 WHERE TargetInstance ISA 'Win32_LogicalDisk'". Have a play with WMI Code Creator to get some code to start with.
 
Unhiding The Hidden Forms!!!

Hi!
Well, I'm almost done with my project. The only functionality I want to add more is hiding and unhiding my form. The idea is that when application is started, a form appears. When I press a start button in the form, the form hides and in the background, the service starts. It start working whenever a new drive is plugged in. This all is done. I want that using some specific key combinations e.g. ctrl+alt+del+u, the hidden form may appear again, and I may stop the service or hide it again. Any help please!
Thanks.
 
Back
Top