重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
Set fso = CreateObject("scripting.filesystemobject")
创新互联是少有的成都做网站、成都网站建设、营销型企业网站、小程序定制开发、手机APP,开发、制作、设计、买链接、推广优化一站式服务网络公司,自2013年起,坚持透明化,价格低,无套路经营理念。让网页惊喜每一位访客多年来深受用户好评
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 '如果是可移动磁盘
Msgbox ID.DriveLetter ":\" '读出盘符
End If
Next
依次读出ds中每个项的VolumeLabel属性就可以了Dim ds() As System.IO.DriveInfods = System.IO.DriveInfo.GetDrives();
可以像脚本一样,创建一个FSO对象。
吧一下代码稍微按照你的方法修改一下再使用即可。
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 "错误!盘符不存在"
end if
以下内容为网络提供,但我自己验证可行,供你参考.
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("请您现在插入U盘至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("请选择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("老四,请输入要保存的字符串", "输入信息", "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("请选择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中增加一个Drive控件
当H盘或更多的磁盘增加时,分区数将大于6个,即: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,获取各个信息部分。
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 + " " '驱动器类型
TextBox1.Text = TextBox1.Text + ds(i).Name + " " '盘符(驱动器名)
TextBox1.Text = TextBox1.Text + ds(i).IsReady.ToString + " " '是否就绪
If ds(i).IsReady = True Then
TextBox1.Text = TextBox1.Text + ds(i).VolumeLabel + " " '卷标
TextBox1.Text = TextBox1.Text + ds(i).TotalSize.ToString + " " '驱动器容量
TextBox1.Text = TextBox1.Text + ds(i).TotalFreeSpace.ToString '驱动器可用容量
End If
TextBox1.Text = TextBox1.Text + vbNewLine
Next