Cara Bikin otomatis scan Saat Flesdisk di pasang

ok langsung saja gan...

pertama :
pake komponen "SysInfo" di VB sbg contoh, coba bikin project baru di vb, trus tambahkan komponen SysInfo caranya pd project ente teken Ctrl+T, trus cari komponen "Microsoft SysInfo Control 6.0" kasih centang trus OK, selanjutnya seret komponen sysinfo ke form, nah skarang tinggal gunakan aja event DeviceArrival seperti kode di bawah:


Option Explicit

Private Const DRIVE_REMOVABLE As Long = 2

Private Sub SysInfo1_DeviceArrival(ByVal DeviceType As Long,
ByVal DeviceID As Long, ByVal DeviceName As String, ByVal DeviceData As Long)
If DeviceType = DRIVE_REMOVABLE Then
'ini bagian kode yg akan dieksekusi ketika device bertipe drive
removable terdeteksi
'sbg contoh kode menampilkan msgbox, dan bisa kamu ganti dg kode
program kamu ;)
MsgBox "Eh!!! ada plesdis tuh nongol di kompi ente :D", vbExclamation
End If
End Sub
kalo ga mau pake komponen, pake api + timer caranya tambahin timer di form dgn nama Timer1, set property intervalnya jadi 1000 (atau terserah). sourco code nya seperti di bawah ini :

Option Explicit

Private Declare Function GetLogicalDriveStringsW Lib "kernel32.dll"
(ByVal nBufferLength As Long, ByVal lpBuffer As Long) As Long
Private Declare Function GetDriveTypeW Lib "kernel32" (ByVal nDrive As Long)
As Long

Private Const DRIVE_REMOVABLE As Long = 2
Private Const MAX_PATH As Long = 260

Private mLastDrives As String

Private Sub Form_Load()
mLastDrives = Join(GetDrives, "|")
End Sub

Private Sub Timer1_Timer()

Dim Drive() As String
Dim Drives As String
Dim i As Long

On Error GoTo Out

Drive = GetDrives
Drives = Join(Drive, "|")

If Len(Drives) = Len(mLastDrives) Then
Exit Sub
ElseIf Len(Drives) < Len(mLastDrives) Then
mLastDrives = Drives Exit Sub End If For i = 0 To UBound(Drive)
If InStr(1, mLastDrives, Drive(i)) = 0 Then
If GetDriveTypeW(StrPtr(Drive(i))) = DRIVE_REMOVABLE Then
MsgBox "Ada flashdisk masuk gan -> " & Drive(i), vbInformation
End If
End If
Next i

mLastDrives = Drives

DoEvents

Out:
End Sub

Private Function GetDrives() As String()

Dim lpBuffer As String
Dim nLong As Long

lpBuffer = String$(MAX_PATH, 0)
nLong = GetLogicalDriveStringsW(MAX_PATH, StrPtr(lpBuffer))

lpBuffer = Left$(lpBuffer, nLong - 1)
GetDrives = Split(lpBuffer, vbNullChar)

End Function

Kedua ::: Compile di mana aja, terus jalanin programnya.
Ketiga ::: Kalo udah jalan, ketika flashdisk masuk maka akan muncul pesan.

akhirnya jadi tuh scan flasdisk secara otomatis ...

========= SELAMAT MENCOBA ========

 

Popular Posts

ShoutBox


ShoutMix chat widget

About Me

Foto Saya
Dadang Supriyadi
Lihat profil lengkapku

Pengikut