How to use USB in visual basic so that I can transfer Music files to and from my mp3 player like rio music manager ?

I think I asked something like this a while back but I would really appreicate more help, I have the mp3 player but I cant seem to find any info on model or make except it says hongye when you turn it on.

LVL 23
Shane Russell2nd Line Desktop SupportAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

HarperBenCommented:
mOST USB MP3 PLAYER INSTALL A REMOVABLE STORAGE DRIVE.
You will see a new drive say "E:\" when your player is plugged in.
You can then use file copy and other file manipulation methods to transfer the files to and from that removable drive

Shane Russell2nd Line Desktop SupportAuthor Commented:
I want to make something simmiliar to Rio Music manager with the interface that detects which drive it uses because obviously different systems will assign different drive letters because of how the systems are configured.

Also I have never used USB when programming or transferring files so I would obviously need help with this.

Thanks
ShelfieldCollegeCommented:
You could use something which loops through the available removeable drives and looks for a particular file on the RIO device, or a particular directory which should exist and use this to identify which drive letter to use.  i dont have one myself so I cant test it sorry, but it's a thought anyways

Cheers

-Matt-
Your Guide to Achieving IT Business Success

The IT Service Excellence Tool Kit has best practices to keep your clients happy and business booming. Inside, you’ll find everything you need to increase client satisfaction and retention, become more competitive, and increase your overall success.

Shane Russell2nd Line Desktop SupportAuthor Commented:
I have thought about all of this stuff already I am looking for examples , I mean how do I test it is a USB drive or actually make a GUI that can interface with the USB disk / mp3 player ??
HarperBenCommented:
OK,
This is a vbscript that will find the usb drive for you, and get the letter.
It will need modification to run in vb probrably.

Just save it in a text file with extension .vbs and run it to see what letter your usb drive is.
Let me know how you go.

strComputer = "."
count = 1
Dim test(40)
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colDevices = objWMIService.ExecQuery _
    ("Select * From Win32_USBControllerDevice")
Dim usbstring
For Each objDevice in colDevices
    strDeviceName = objDevice.Dependent
    strQuotes = Chr(34)
    strDeviceName = Replace(strDeviceName, strQuotes, "")
    arrDeviceNames = Split(strDeviceName, "=")
    strDeviceName = arrDeviceNames(1)
    Set colUSBDevices = objWMIService.ExecQuery _
        ("Select * From Win32_PnPEntity Where DeviceID = '" & strDeviceName & "'")
    For Each objUSBDevice in colUSBDevices
          test(count) = "" & objusbdevice.name & ""
          'MsgBox test(count)'
          count = count +1
          Next    
Next
strComputer = "."
Set wmiServices = GetObject _
    ("winmgmts:{impersonationLevel=Impersonate}!//" & strComputer)
Set wmiDiskDrives = wmiServices.ExecQuery _
    ("SELECT Caption, DeviceID FROM Win32_DiskDrive")
 For Each wmiDiskDrive In wmiDiskDrives
  for i = 0 to count
        if wmiDiskDrive.caption = test(i) then'"031125 Flash Disk USB Device" then
        strEscapedDeviceID = Replace _
        (wmiDiskDrive.DeviceID, "\", "\\", 1, -1, vbTextCompare)
    Set wmiDiskPartitions = wmiServices.ExecQuery _
        ("ASSOCIATORS OF {Win32_DiskDrive.DeviceID=""" & _
    strEscapedDeviceID & """} WHERE AssocClass = " & _
        "Win32_DiskDriveToDiskPartition")
     For Each wmiDiskPartition In wmiDiskPartitions
            Set wmiLogicalDisks = wmiServices.ExecQuery _
            ("ASSOCIATORS OF {Win32_DiskPartition.DeviceID=""" & _
                wmiDiskPartition.DeviceID & """} WHERE AssocClass = " & _
                    "Win32_LogicalDiskToPartition")
         For Each wmiLogicalDisk In wmiLogicalDisks
            founddrive = wmiLogicalDisk.DeviceID
        Next
    Next
   else
   end if
   next
Next
if founddrive = "" then
      msgbox "sorry no usb drive found"
else
    msgbox "Found USB disk on drive " & founddrive
end if

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ShelfieldCollegeCommented:
It's a bit hard for me to help without having one to test with, when the device creates the removeable drive if it gives it a name then you could find it's drive letter from this. For example, my usb stick shows up as a drive called "MightyDrive" when it connects, so you could loop through all the available drives and check for one labeled as whatever it shows up as ("MightyDrive" in my case).

Try this:

Add a reference to Microsoft Scripting Runtime in your project

and try this code:

in the Declarations section put this
Dim iFileSys As New Scripting.FileSystemObject

then in the form load event try this:

For Each lDrive In lFileSys.Drives
  if (lFileSys.DriveExists(lDrive)) Then
    if lDrive.VolumeName = "MightyDrive" && lDrive.DriveType = 1 then
       'DriveType of 1 = Removable, not really needed if you're checking the volumename as well
       'However thought it would be worth while showing you how to do it.
       msgbox "Mighty Drive Letter Is: " & lDrive.DriveLetter
    end if
  end if
next

This should work, i typed it in whilst looking at the code on another window so appologies for any errors, basically it will find the drive letter for any removeable drive whos volume name is "MightyDrive" so change that bit to be the volume name used when your RIO connect and it should give you the drive letter for the RIO.

As for interacting with it, basicaly it's just used like a normal storage method, e.g. files and folders. so you could use the normal methods for looping through files etc If you want an example of how to list all files in a given directory then just say and I'll post one.

Please note the above code isn't entirely my own, the majority of it was simply adapted from an example found on a website somewhere.

Cheers

-Matt-
Shane Russell2nd Line Desktop SupportAuthor Commented:
If you could post back an example of how to loop through files that would be very much appreicated however I was hoping for a more GUI approuch ie having an explorer style window from where I can make my program something like Nero Burning ROM where users have the explorer style view of whats on the mp3 player on the left hand side and my computer and all the drives on the right hand side so they can drag and drop files or music on to the USB memory stick / mp3 player.
HarperBenCommented:
I think what gecko_au2003 is trying to do is find out what the removable drive is without
knowing the name of the drive and without asking the user to identify the drive for him
So that it is not written specificaly to suit his drive but can be dristributed.

Windows names the removable drive similar or the same to what it is named in the device manager,
For this reason I have peiced together the code to check the names of all usb devices,
then go and check the drive names on the computer, and when it finds a match then it must be a usb drive.
It then finds and gives you the drive letter for that drive.

Otherwise simpley searching for a drive name will not help you as you
will need to ask the user what drive name to look for, and if you are going to do that,
you may as well just ask them what the drive letter is and forget doing anything in code.
Shane Russell2nd Line Desktop SupportAuthor Commented:
exactly HarperBen , I wouldnt go to all this hassle to do it  in coding just do end up with that. If I wanted something smiple I would of just downloaded Rio music manager or another application but I also want to know how it is done exactly that is why I ask the questions on EE in the first place.

No offence to anyone on here intended and maybe it is the way I explain things because I have a hard time with explaining things but I am glad that at least HarperBen understood where I am coming from.

Thanks HarperBen, much appriecated, if it is ok with you HarperBen and everyone else I would like to leave this open for longer to see if I get an actual Visual Basic version of this.

I will do a split points because you deserve a good chunk of the points there Harper Ben !!!

Thanks again !!
HarperBenCommented:
I will modify the code i sent you so that it will work in vb/vba, tonight.
Should be almost the same, just need to dimension variables and a few other things probrably
No probs, glad to help.
Ben
HarperBenCommented:

I have vb.net, which i have almost no idea how to use.
My exposure to Visual basic has been strictly VBA and VBScript.

It was harder to work out how to make a module in vb.net and get it to run that it was to work out the code.
I must take a closer look at vb.net, I have the vb side of it under control, but it all gives me a headache when i see the vb.net gui and i dont know where to start.

Anyway, if vb.net will run this code, I assume what ever version of VB you have will have a go too.
Let me know how you go.
Ben

sub findusbdrive
Dim strComputer As String
        Dim count As Integer
        Dim test(40)
        Dim objWMIService
        Dim colDevices
        Dim objDevice
        Dim strDeviceName
        Dim strQuotes
        Dim arrDeviceNames
        Dim colUSBDevices
        Dim objusbdevice
        Dim wmiServices
        Dim wmiDiskDrives
        Dim wmiDiskDrive
        Dim strEscapedDeviceID
        Dim wmiDiskPartitions
        Dim wmiDiskPartition
        Dim wmiLogicalDisks
        Dim wmiLogicalDisk
        Dim founddrive
        Dim i




        strComputer = "."
        Count = 1

        objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
        colDevices = objWMIService.ExecQuery _
            ("Select * From Win32_USBControllerDevice")
        Dim usbstring
        For Each objDevice In colDevices
            strDeviceName = objDevice.Dependent
            strQuotes = Chr(34)
            strDeviceName = Replace(strDeviceName, strQuotes, "")
            arrDeviceNames = Split(strDeviceName, "=")
            strDeviceName = arrDeviceNames(1)
            colUSBDevices = objWMIService.ExecQuery _
                ("Select * From Win32_PnPEntity Where DeviceID = '" & strDeviceName & "'")
            For Each objusbdevice In colUSBDevices
                test(Count) = "" & objusbdevice.Name & ""
                'MsgBox test(count)'
                Count = Count + 1
            Next
        Next
        strComputer = "."
        wmiServices = GetObject _
            ("winmgmts:{impersonationLevel=Impersonate}!//" & strComputer)
        wmiDiskDrives = wmiServices.ExecQuery _
            ("SELECT Caption, DeviceID FROM Win32_DiskDrive")
        For Each wmiDiskDrive In wmiDiskDrives
            For i = 0 To Count
                If wmiDiskDrive.Caption = test(i) Then '"031125 Flash Disk USB Device" then
                    strEscapedDeviceID = Replace _
                    (wmiDiskDrive.DeviceID, "\", "\\", 1, -1, vbTextCompare)
                    wmiDiskPartitions = wmiServices.ExecQuery _
                        ("ASSOCIATORS OF {Win32_DiskDrive.DeviceID=""" & _
                    strEscapedDeviceID & """} WHERE AssocClass = " & _
                        "Win32_DiskDriveToDiskPartition")
                    For Each wmiDiskPartition In wmiDiskPartitions
                        wmiLogicalDisks = wmiServices.ExecQuery _
                        ("ASSOCIATORS OF {Win32_DiskPartition.DeviceID=""" & _
                            wmiDiskPartition.DeviceID & """} WHERE AssocClass = " & _
                                "Win32_LogicalDiskToPartition")
                        For Each wmiLogicalDisk In wmiLogicalDisks
                            founddrive = wmiLogicalDisk.DeviceID
                        Next
                    Next
                Else
                End If
            Next
        Next
        If founddrive = "" Then
            MsgBox("sorry no usb drive found")
        Else
            MsgBox("Found USB disk on drive " & founddrive)
        End If

    End Sub
HarperBenCommented:

When it comes to file manipulation , moving deleting copying etc.
Check out this link
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon98/html/vbconManipulatingFiles.asp
and checkout vb's help files on FSO (File System Objects)
HarperBenCommented:
All done? we should close this question.
Shane Russell2nd Line Desktop SupportAuthor Commented:
I appologize for taking so long but I havent had internet access at home for quite some time because I am trying to get my own desktop computer up and running and it has been hectic at work as I just started my new job so havent had much time to do much of anything except work lol.

Anyway all of your help is very much appreicated and thank you once again for the help and the patience !

Kind regards

Gecko
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Fonts Typography

From novice to tech pro — start learning today.