【实例简介】
【实例截图】
【核心代码】
using System;
using System.Collections.Generic;
using ponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Microsoft.Win32;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace DectUsb
{
public partial class Form1 : Form
{
public const int WM_DEVICECHANGE = 0x219;//U盘插入后,OS的底层会自动检测到,然后向应用程序发送“硬件设备状态改变“的消息
public const int DBT_DEVICEARRIVAL = 0x8000; //就是用来表示U盘可用的。一个设备或媒体已被插入一块,现在可用。
public const int DBT_CONFIGCHANGECANCELED = 0x0019; //要求更改当前的配置(或取消停靠码头)已被取消。
public const int DBT_CONFIGCHANGED = 0x0018; //当前的配置发生了变化,由于码头或取消固定。
public const int DBT_CUSTOMEVENT = 0x8006; //自定义的事件发生。 的Windows NT 4.0和Windows 95:此值不支持。
public const int DBT_DEVICEQUERYREMOVE = 0x8001; //审批要求删除一个设备或媒体作品。任何应用程序也不能否认这一要求,并取消删除。
public const int DBT_DEVICEQUERYREMOVEFAILED = 0x8002; //请求删除一个设备或媒体片已被取消。
public const int DBT_DEVICEREMOVECOMPLETE = 0x8004; //一个设备或媒体片已被删除。
public const int DBT_DEVICEREMOVEPENDING = 0x8003; //一个设备或媒体一块即将被删除。不能否认的。
public const int DBT_DEVICETYPESPECIFIC = 0x8005; //一个设备特定事件发生。
public const int DBT_DEVNODES_CHANGED = 0x0007;//一种设备已被添加到或从系统中删除。
public const int DBT_QUERYCHANGECONFIG = 0x0017;//许可是要求改变目前的配置(码头或取消固定)。
public const int DBT_USERDEFINED = 0xFFFF;//此消息的含义是用户定义的
const int WM_SETTINGCHANGE = 0x001A;
const int HWND_BROADCAST = 0xffff;
IntPtr result1;
public enum SendMessageTimeoutFlags : uint
{
SMTO_NORMAL = 0x0000,
SMTO_BLOCK = 0x0001,
SMTO_ABORTIFHUNG = 0x0002,
SMTO_NOTIMEOUTIFNOTHUNG = 0x0008
}
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern IntPtr SendMessageTimeout(
IntPtr windowHandle,
uint Msg,
IntPtr wParam,
IntPtr lParam,
SendMessageTimeoutFlags flags,
uint timeout,
out IntPtr result
);
public void ChangeReg()
{
//通知所有打开的程序注册表以修改
SendMessageTimeout(new IntPtr(HWND_BROADCAST), WM_SETTINGCHANGE, IntPtr.Zero, IntPtr.Zero, SendMessageTimeoutFlags.SMTO_NORMAL, 1000, out result1);
}
//[DllImport("shell32.dll")]
//public static extern void SHChangeNotify(uint wEventId, uint uFlags, IntPtr dwItem1, IntPtr dwItem2);
public Form1()
{
InitializeComponent();
}
public void HidDisk(int n)
{
RegistryKey rgK = Registry.CurrentUser;
RegistryKey rgK1 = rgK.OpenSubKey("Software", true);
RegistryKey rgK2 = rgK1.OpenSubKey("Microsoft", true);
RegistryKey rgK3 = rgK2.OpenSubKey("Windows", true);
RegistryKey rgK4 = rgK3.OpenSubKey("CurrentVersion", true);
RegistryKey rgK5 = rgK4.OpenSubKey("Policies", true);
RegistryKey rgK6 = rgK5.CreateSubKey("Explorer");
rgK6.SetValue("NoDrives", n);
int intvalue = (int)rgK6.GetValue("NoDrives");
rgK1.Close();
rgK2.Close();
rgK3.Close();
rgK4.Close();
rgK5.Close();
rgK6.Close();
MessageBox.Show(intvalue.ToString());
//Process[] ExProcess = System.Diagnostics.Process.GetProcessesByName("Explorer.EXE");
//foreach (Process p in ExProcess)
//{
// p.Kill();
// p.WaitForExit();
// p.Close();
//}
//System.Threading.Thread.Sleep(10000);
//Process pr = new Process();
//ProcessStartInfo ps = new ProcessStartInfo("Explorer.exe");
//pr.StartInfo = ps;
//pr.Start();
//SHChangeNotify(0x8000000, 0, IntPtr.Zero, IntPtr.Zero);
ChangeReg();
}
protected override void WndProc(ref Message m)
{
try
{
if (m.Msg == WM_DEVICECHANGE)
{
switch (m.WParam.ToInt32())
{
case WM_DEVICECHANGE:
break;
case DBT_DEVICEARRIVAL://U盘插入
DriveInfo[] uin = DriveInfo.GetDrives();
foreach (DriveInfo drive in uin)
{
if (drive.DriveType == DriveType.Removable)
{
listBox1.Items.Add("U盘已插入,盘符为:" drive.Name.ToString());
// if ((drive.Name.ToString())=="K:\\")
// {
// HidDisk(16);
// }
// break;
switch (drive.Name.ToString())
{
case "G:\\":
HidDisk(64);
break;
case "H:\\":
HidDisk(192);
break;
case "I:\\":
HidDisk(384);
break;
case "J:\\":
HidDisk(768);
break;
case "K:\\":
HidDisk(1536);
break;
case "M:\\":
HidDisk(3072);
break;
case "N:\\":
HidDisk(6144);
break;
case "O:\\":
HidDisk(12288);
break;
default:
break;
}
}
}
break;
case DBT_CONFIGCHANGECANCELED:
break;
case DBT_CONFIGCHANGED:
break;
case DBT_CUSTOMEVENT:
break;
case DBT_DEVICEQUERYREMOVE:
break;
case DBT_DEVICEQUERYREMOVEFAILED:
break;
case DBT_DEVICEREMOVECOMPLETE: //U盘卸载
listBox1.Items.Add("U盘被拔出!");
break;
case DBT_DEVICEREMOVEPENDING:
break;
case DBT_DEVICETYPESPECIFIC:
break;
case DBT_DEVNODES_CHANGED:
break;
case DBT_QUERYCHANGECONFIG:
break;
case DBT_USERDEFINED:
break;
default:
break;
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
base.WndProc(ref m);
}
}
}