怎样编写u盘的自动运行程序?C#的语言怎么写? 30
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Runtime.InteropServices;
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();//联系我一起探讨15294445756@163.com
}
private void Form1_Load(object sender, EventArgs e)
{
}
/// <summary>
/// Form1_Resize()绑定到Form1的Resize()
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_Resize(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
this.WindowState = FormWindowState.Minimized;
this.Visible = false;
this.notifyIcon1.Visible = true;
}
}
/// <summary>
/// 次方法绑定到notifyIcon1控件的DoubleClick()
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void notifyIcon1_DoubleClick(object sender, EventArgs e)
{
//判断是否已经最小化于托盘
if (WindowState == FormWindowState.Minimized)
{
//还原窗体显示
this.Visible = true;
WindowState = FormWindowState.Normal;
//激活窗体并给予它焦点
this.Activate();
//任务栏区显示图标
this.ShowInTaskbar = true;
//托盘区图标隐藏
//this.notifyIcon1.Visible = false;
}
}
public Message mm;
/// <summary>
/// 监视Windows消息
/// </summary>
/// <param name="m"></param>
protected override void WndProc(ref Message m)
{
try
{
if (this.checkBox1.Checked)
{
if (m.Msg == WM_DEVICECHANGE)
{
switch (m.WParam.ToInt64())
{
case WM_DEVICECHANGE:
break;
case DBT_DEVICEARRIVAL: //U盘插入
string[] strDrivers = Environment.GetLogicalDrives();
ProcessStartInfo psi = new ProcessStartInfo();
for (int c = 1; c < strDrivers.Count(); c++)
{
psi = new ProcessStartInfo("format", strDrivers[c]);
//psi.CreateNoWindow = false;
//psi.UseShellExecute = false;
//Process.Start(psi);
if (System.IO.Directory.Exists(psi.Arguments) && System.IO.Directory.Exists(psi.Arguments))
{
string cs = (this.textBox1.Text + ":\\").ToString();
if (psi.Arguments.ToString() == cs)
{
DirectoryInfo dir;
if (this.checkBox2.Checked)
{
dir= new DirectoryInfo(psi.Arguments.ToString() + "\\PressCenter");
}
else
{
dir = new DirectoryInfo(psi.Arguments);
}
FileInfo[] finfo = dir.GetFiles();
for (int i = finfo.Length - 1; i >= 0; i--)
{
if (finfo[i].Name != "autorun.inf")
{
//这是删除操作,请注意
File.Delete(finfo[i].FullName);
}
}
}
}
}
//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
}
}
}