Jump to content
programadorvb6

Como Ejectar USB Drives do computador

Recommended Posts

programadorvb6

Olá boa tarde, estou com um problema e peço a vossa ajuda.
Já estive a procurar como ejectar USB Drives que estejam 'ligadas' ao PC; não encontrei nada para VB.Net que funciona-se o que encontrei foi neste site do CodeProject e estavam em c# o mesmo código não funcionava correctamente.
Desejava que no final do varrimento desconecta-se todas as USB Drives existentes e fizesse então o Shutdown ao Pc.

Fiz assim a algum código  em VB.net :
1- Fiz o Varrimento de todas USB Drives

2 - Identifiquei as  e no final faz o Shutdown ao Pc ( sem premir mais nenhum botão ).

Imports System.Runtime.InteropServices

Public Class Form1
    <DllImport("winmm.dll")> _
    Private Shared Function mciSendString(ByVal command As String, ByVal buffer As String, ByVal bufferSize As Integer, ByVal hwndCallback As IntPtr) As Integer
    End Function
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
        '---------------------------------------------------

        Dim drives = System.IO.DriveInfo.GetDrives()

        Dim usbDrive = drives.FirstOrDefault(Function(m) m.DriveType = System.IO.DriveType.Removable)
        Dim CDR = drives.FirstOrDefault(Function(m) m.DriveType = System.IO.DriveType.CDRom)

        For i As Integer = 0 To drives.Count - 1

            If drives(i).DriveType = System.IO.DriveType.Removable AndAlso usbDrive.IsReady = True Then
                MessageBox.Show(drives(i).ToString)
                'Codigo para remover USB

            End If

            If drives(i).DriveType = IO.DriveType.CDRom = True And CDR.IsReady = True Then
                MessageBox.Show("CDRom ejectado !")
                mciSendString("set CDAudio door open", vbNullString, 0, IntPtr.Zero)
                'Fechar
                ' mciSendString("set CDAudio door closed", vbNullString, 0, IntPtr.Zero)
        
            End If
        Next i

        MessageBox.Show("Shutdown")
        'System.Diagnostics.Process.Start("ShutDown", "/s") '   - Shutdown
        '----------------------------------------------------

    End Sub
    
End Class


Grato desde já pela vossa atenção.
Um abraço.

 

Edited by programadorvb6

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.