using System;
using System.Linq;
using System.Windows.Forms;
using System.Diagnostics;
namespace up
{
public partial class Form1 : Form
{
public const int WM_DEVICECHANGE = 0x219;
public const int DBT_DEVICEARRIVAL = 0x8000; //如果m.Msg的值为0x8000那么表示有U盘插入
public const int DBT_CONFIGCHANGECANCELED = 0x0019;
public const int DBT_CONFIGCHANGED = 0x0018;
public const int DBT_CUSTOMEVENT = 0x8006;
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;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
public Message mm;
///
/// 监视Windows消息
///
///
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盘插入
string[] strDrivers = Environment.GetLogicalDrives();
ProcessStartInfo psi = new ProcessStartInfo("format", strDrivers[strDrivers.Count() - 1] + "/q");
psi.CreateNoWindow = false;
Process.Start(psi);
//listBox1.Items.Add("U盘已插入,盘符为:" + strDrivers[strDrivers.Count() - 1]);
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盘卸载
break;
case DBT_DEVICEREMOVEPENDING:
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); //将系统消息传递自父类的WndProc
}
}
}
没有办法实现
不过倒是可以想另外办法(注册表写保护就可以了)
U盘提示格式化?教你一个方法 原来找出故障原因这么简单
那你的程序也格式化了?还有意义么
那即使U盘有重要资料也要被格了????