C#如何实现弹出U盘?
需要一个类EjectUsb,然后通过下面的语句就可以实现U盘弹出。
EjectUsb eju = new EjectUsb(label13.Text); bool result = eju.Eject(); if (result) { MessageBox.Show("弹出成功","提示"); } else { MessageBox.Show("弹出失败","提示"); }
部分代码如下
using System; using System.Collections.Generic; using System.Runtime.InteropServices; using System.Text; using System.Threading; namespace USB管理精灵系统 { public class EjectUsb { //返回设备句柄 [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)] private static extern IntPtr CreateFile( string lpFileName, uint dwDesiredAccess, uint dwShareMode, IntPtr SecurityAttributes, uint dwCreationDisposition, uint dwFlagsAndAttributes, IntPtr hTemplateFile ); [DllImport("kernel32.dll", ExactSpelling = true, SetLastError = true, CharSet = CharSet.Auto)] private static extern bool DeviceIoControl( IntPtr hDevice, uint dwIoControlCode, IntPtr lpInBuffer, uint nInBufferSize, IntPtr lpOutBuffer, uint nOutBufferSize, out uint lpBytesReturned, IntPtr lpOverlapped ); [DllImport("kernel32.dll", ExactSpelling = true, SetLastError = true, CharSet = CharSet.Auto)] private static extern bool DeviceIoControl( IntPtr hDevice, uint dwIoControlCode, byte[] lpInBuffer, uint nInBufferSize, IntPtr lpOutBuffer, uint nOutBufferSize, out uint lpBytesReturned, IntPtr lpOverlapped ); ******* }
类EjectUsb的完整代码下载如下
已售:22
此处内容付费后可见***
支付0.38元查看