Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good afternoon everyone.
I'm new here, I'd love your help.
I wonder if and can change or put a letter in the drive.
Example: I have a USB stick with the name "test" and the letter and assigned automatically by the system right.
I would like to identify the stick through the "Name: test" "VolumeName" and put a specific letter for the drive "driveletter: P"
if the system has not assigned one letter to the unit, put the letter "P".
So whenever I run it would identify the volume with the name "test" and is assigned the letter "p" for the volume.
Thank you in advance.
Visual Basic 2010

What I have tried:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim fso As New Scripting.FileSystemObject()
Dim drv As Scripting.Drive
Dim string_builder As New System.Text.StringBuilder()
Dim asd As String
Dim letra As String

For Each drv In fso.Drives
string_builder.Append( _
drv.DriveLetter & ":" & vbCrLf & _
" Type: " & drv.DriveType.ToString() & vbCrLf)
If drv.IsReady Then
string_builder.Append( _
" File System: " & drv.FileSystem & vbCrLf & _
" Free Space: " & drv.FreeSpace & vbCrLf & _
" Total Size: " & drv.TotalSize & vbCrLf & _
" Volume Name: " & drv.VolumeName & vbCrLf & _
" Serial Number: " & drv.SerialNumber & vbCrLf & _
"--------------------" & vbCrLf)
asd = drv.VolumeName
letra = drv.DriveLetter
If asd = "ws" Then
MsgBox(drv.VolumeName)
drv.DriveLetter = "p"




Else
MsgBox("nao")
End If
Else
string_builder.Append( _
" Not ready" & vbCrLf & _
"--------------------" & vbCrLf)
End If

Next drv


txtDrives.Text = string_builder.ToString()
txtDrives.Select(0, 0)

End Sub

Private Sub txtDrives_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDrives.TextChanged

End Sub
End Class
Posted
Updated 26-May-16 17:41pm

1 solution

One of the ways to identify the thumb drive is using WMI.

You can look at my PowerShell scrip and use it as a pseudo-code:
$removable = Get-WmiObject CIM_StorageVolume$r | where { $_.driveType -eq 2 }

foreach ($drive in $removable) {
   # use $drive.Name, 
   # $drive.Label
   # $drive.DeviceID
   # ...
}

The "type" 2 corresponds to "removable drive". Optical media is not considered "removable", so, in practice you will traverse the set of your USB drives and, perhaps, embedded card reader.

For the documentation on the WMI class please see: CIM_StorageVolume class (Windows)[^].

You also need to learn how to use WMI via .NET. The idea is: you don't use the classes like directly CIM_StorageVolume. You pass then name of this class as a string to System.Management API. For example, in your case, you obtain the WMI class instance via the constructor call:
new ManagementClass("CIM_StorageVolume").

You can start here:
WMI .NET Overview[^].

This is the rudimentary code sample, unrelated to your problem. I just wanted to give you the idea on how API is used: A Simple System.Management Application[^].

—SA
 
Share this answer
 
v4
Comments
h3nr1qu3 28-May-16 16:28pm    
Good afternoon.
I found an interesting code, most could not get satisfactory results alone you could help me.
It changes the letter specifies, not the volume label.
eg if the flash drive is the letter "R: \" to run the command it switches to "p" :) .More when flash drive is plugged into the machine
the system can assign a different letter "R: \" so my intention and assign the letter by name stick "test."

Public Class Form1

    Private Property StringComparer As String

    Private Property objWMIService The Object

    Private Property colVolumes The Object

    Private Sub Button1_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        StringComparer = "."
        objWMIService = GetObject ( "winmgmts" _
            & "{ImpersonationLevel = impersonate}! \\" & StringComparer & "\ root \ cimv2")


        colVolumes = objWMIService.ExecQuery ("Select * from Win32_Volume Where Name = 'R:\\'")

        For Each objVolume In colVolumes
            objVolume.DriveLetter = "P"
            objVolume.Put_ ()
        Next

    end Sub


end Class
Sergey Alexandrovich Kryukov 28-May-16 20:44pm    
It won't work. In this code, "R:\" is hard-coded. But you don't really know the drive letter in advance and cannot rely on that. The code I suggested does work; I use it on regular basis.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900