检测u盘插拔状态并读写的方法

上传人:小** 文档编号:57384321 上传时间:2018-10-21 格式:DOC 页数:4 大小:87.50KB
返回 下载 相关 举报
检测u盘插拔状态并读写的方法_第1页
第1页 / 共4页
检测u盘插拔状态并读写的方法_第2页
第2页 / 共4页
检测u盘插拔状态并读写的方法_第3页
第3页 / 共4页
检测u盘插拔状态并读写的方法_第4页
第4页 / 共4页
亲,该文档总共4页,全部预览完了,如果喜欢就下载吧!
资源描述

《检测u盘插拔状态并读写的方法》由会员分享,可在线阅读,更多相关《检测u盘插拔状态并读写的方法(4页珍藏版)》请在金锄头文库上搜索。

1、本文来介绍一下,利用 VB.Net 检测 U 盘插拔状态并读写的方法 代码如下: 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_DEVICEQ

2、UERYREMOVE = &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 Con

3、st DBT_USERDEFINED = &HFFFF Protected Overrides Sub WndProc(ByRef m As System.Windows.Form s.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

4、In s If drive.DriveType = DriveType.Removable Th en 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_DEVICEQUERYREMOV

5、E 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 My

6、Base.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, ByVa l e As System.EventArgs) Handles BtnWrite.Click If ComboBox1

7、.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(“W

8、rite 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 Co

9、mboBox1.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.Re adToEnd, 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 注意:Winform 需要两个 Button、一个 Combox、一个 ListBox 控件。

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 商业/管理/HR > 管理学资料

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号