Set fso = CreateObject("scripting.filesystemobject")
創(chuàng)新互聯(lián)是少有的成都做網(wǎng)站、成都網(wǎng)站建設(shè)、營(yíng)銷型企業(yè)網(wǎng)站、小程序定制開發(fā)、手機(jī)APP,開發(fā)、制作、設(shè)計(jì)、買鏈接、推廣優(yōu)化一站式服務(wù)網(wǎng)絡(luò)公司,自2013年起,堅(jiān)持透明化,價(jià)格低,無套路經(jīng)營(yíng)理念。讓網(wǎng)頁(yè)驚喜每一位訪客多年來深受用戶好評(píng)
Set disks = fso.Drives
For Each disk In disks '枚舉磁盤
Set ID = fso.GetDrive(fso.GetDriveName(disk))
If ID.drivetype = 1 And disk.IsReady = True Then '如果是可移動(dòng)磁盤
Msgbox ID.DriveLetter ":\" '讀出盤符
End If
Next
依次讀出ds中每個(gè)項(xiàng)的VolumeLabel屬性就可以了Dim ds() As System.IO.DriveInfods = System.IO.DriveInfo.GetDrives();
可以像腳本一樣,創(chuàng)建一個(gè)FSO對(duì)象。
吧一下代碼稍微按照你的方法修改一下再使用即可。
idr="C:" '要判斷的盤符
s=0
Set fso = CreateObject("Scripting.FileSystemObject")
Set drs = fso.Drives
For Each dr in drs
if dr=idr then s=1
Next
if s=1 then
msgbox "存在"
else
msgbox "錯(cuò)誤!盤符不存在"
end if
以下內(nèi)容為網(wǎng)絡(luò)提供,但我自己驗(yàn)證可行,供你參考.
Imports System.IO
Public Class Form1
Public Const WM_DEVICECHANGE = H219
Public Const DBT_DEVICEARRIVAL = H8000
Public Const DBT_CONFIGCHANGECANCELED = H19
Public Const DBT_CONFIGCHANGED = H18
Public Const DBT_CUSTOMEVENT = H8006
Public Const DBT_DEVICEQUERYREMOVE = H8001
Public Const DBT_DEVICEQUERYREMOVEFAILED = H8002
Public Const DBT_DEVICEREMOVECOMPLETE = H8004
Public Const DBT_DEVICEREMOVEPENDING = H8003
Public Const DBT_DEVICETYPESPECIFIC = H8005
Public Const DBT_DEVNODES_CHANGED = H7
Public Const DBT_QUERYCHANGECONFIG = H17
Public Const DBT_USERDEFINED = HFFFF
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = WM_DEVICECHANGE Then
Select Case m.WParam
Case WM_DEVICECHANGE
Case DBT_DEVICEARRIVAL 'U盤插入
ComboBox1.Items.Clear()
Dim s() As DriveInfo = DriveInfo.GetDrives
For Each drive As DriveInfo In s
If drive.DriveType = DriveType.Removable Then
ListBox1.Items.Add("U盤已插入!盤符為:" + drive.Name.ToString())
ComboBox1.Items.Add(drive.Name)
End If
Next
BtnWrite.Enabled = True
BtnRead.Enabled = True
Case DBT_CONFIGCHANGECANCELED
Case DBT_CONFIGCHANGED
Case DBT_CUSTOMEVENT
Case DBT_DEVICEQUERYREMOVE
Case DBT_DEVICEQUERYREMOVEFAILED
Case DBT_DEVICEREMOVECOMPLETE 'U盤卸載
ListBox1.Items.Add("U盤卸載!")
BtnWrite.Enabled = False
BtnRead.Enabled = False
Case DBT_DEVICEREMOVEPENDING
Case DBT_DEVICETYPESPECIFIC
Case DBT_DEVNODES_CHANGED
Case DBT_QUERYCHANGECONFIG
Case DBT_USERDEFINED
End Select
End If
MyBase.WndProc(m)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ListBox1.Items.Add("請(qǐng)您現(xiàn)在插入U(xiǎn)盤至USB接口!")
End Sub
Private Sub BtnWrite_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnWrite.Click
If ComboBox1.Text = "" Then
MsgBox("請(qǐng)選擇U盤盤符!", MsgBoxStyle.Exclamation, "Warn")
Else
Dim Writer As StreamWriter = Nothing
Try
Dim fileName As String = ComboBox1.Text + "Test.txt"
Writer = New StreamWriter(fileName)
Writer.WriteLine(InputBox("老四,請(qǐng)輸入要保存的字符串", "輸入信息", "Input then Test String! hehe!"))
MsgBox("Write to " + fileName + " Success!")
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Write 失敗")
Finally
If Writer IsNot Nothing Then Writer.Close()
End Try
End If
End Sub
Private Sub BtnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnRead.Click
If ComboBox1.Text = "" Then
MsgBox("請(qǐng)選擇U盤盤符!", MsgBoxStyle.Exclamation, "Warn")
Else
Dim Reader As StreamReader = Nothing
Try
Dim fileName As String = ComboBox1.Text + "Test.txt"
Reader = New StreamReader(fileName)
MsgBox("Read from " + fileName + vbCrLf + Reader.ReadToEnd, MsgBoxStyle.Information, "Info")
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Read 失敗")
Finally
If Reader IsNot Nothing Then Reader.Close()
End Try
End If
End Sub
End Class
還有一種方法哦.
首先在Form中增加一個(gè)Drive控件
當(dāng)H盤或更多的磁盤增加時(shí),分區(qū)數(shù)將大于6個(gè),即:A,C,D,E,F,G,(H)
輸入代碼如下:
Private
Sub
form_load()
Timer1.Enabled
=
True
Timer1.Interval
=
500
Timer2.Enabled
=
False
Timer2.Interval
=
500
Drive1.Visible
=
False
End
Sub
Private
Sub
Timer1_Timer()
Dim
a
As
Integer
Drive1.Refresh
a
=
Drive1.ListCount
If
a
6
Then
Form1.Show
Timer2.Enabled
=
True
Timer1.Enabled
=
False
End
If
End
Sub
Private
Sub
Timer2_Timer()
Dim
a
As
Integer
Drive1.Refresh
a
=
Drive1.ListCount
If
a
=6
Then
Form1.Hide
Timer1.Enabled
=
True
End
If
End
Sub
首先使用 System.IO.DriveInfo.GetDrives()獲取System.IO.DriveInfo,存入ds()
然后遍歷ds,獲取各個(gè)信息部分。
Dim ds() As System.IO.DriveInfo = System.IO.DriveInfo.GetDrives()
For i As Integer = 0 To ds.Length - 1
TextBox1.Text = TextBox1.Text + ds(i).DriveType.ToString + " " '驅(qū)動(dòng)器類型
TextBox1.Text = TextBox1.Text + ds(i).Name + " " '盤符(驅(qū)動(dòng)器名)
TextBox1.Text = TextBox1.Text + ds(i).IsReady.ToString + " " '是否就緒
If ds(i).IsReady = True Then
TextBox1.Text = TextBox1.Text + ds(i).VolumeLabel + " " '卷標(biāo)
TextBox1.Text = TextBox1.Text + ds(i).TotalSize.ToString + " " '驅(qū)動(dòng)器容量
TextBox1.Text = TextBox1.Text + ds(i).TotalFreeSpace.ToString '驅(qū)動(dòng)器可用容量
End If
TextBox1.Text = TextBox1.Text + vbNewLine
Next