u盘自动运行怎么写

1.怎样编写u盘的自动运行程序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_ = 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();//联系我一起探讨[email protected] } private void Form1_Load(object sender, EventArgs e) { } ////// Form1_Resize()绑定到Form1的Resize() ////// /// private void Form1_Resize(object sender, EventArgs e) { if (this.WindowState == FormWindowState.Minimized) { this.WindowState = FormWindowState.Minimized; this.Visible = false; this.notifyIcon1.Visible = true; } } ////// 次方法绑定到notifyIcon1控件的DoubleClick() ////// /// 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; ////// 监视Windows消息 ////// 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_: 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 } }} 。
2.U盘自动运行 编写autorun[autorun]
【u盘自动运行怎么写】Open=*.* (你要运行的程序比如“***.rar”)
shell\open=打开(&O)
shell\open\Command=*.*
shell\open\Default=1
shell\explore=资源管理器(&X)
shell\explore\Command=*.*
shell\explore=自动播放(&P)
Shell\Explorer\Command=Explorer.exe
保存为 autorun.inf 文件放到你U盘的根目录下,这样以后无论你在电脑上双击,右键“任务管理”,右键“打开”都是运行(*.*)你U盘里的程序