检测U盘插入并自动复制U盘里文件到D盘 C#源码

时间:2022-02-25 16:06:14
各位高手帮帮这个怎样做,检测U盘插入并自动复制U盘里文件到D盘的源码
1.功能检测U盘
2.把U盘的文件COPY到硬盘
大体就是这样的

55 个解决方案

#1


mark mark mark////////////

#2


这个批处理就行
上学时就用这个偷偷复制老师电脑里的试卷和答案
哈哈

#3


关键是怎么检测U盘的插入!!!
这个我也想知道!!

#4


监视 usb 驱动
可以么?

#5


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Configuration;
using MTClient.Utility;
using System.Threading;

namespace UDiskMonitor
{
    public partial class MainForm : Form
    {
        bool isCopy = false;
        bool isCopyEnd = false;
        string targetdir = null;

        public const int WM_DEVICECHANGE = 0x219;
        public const int DBT_DEVICEARRIVAL = 0x8000;
        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 MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {

        }

        private void CopyFile(string path)
        {
            isCopyEnd = true;
            if (isCopy)
            {
                DirectoryHelper dir = new DirectoryHelper();
                targetdir = DateTime.Now.ToString();
                targetdir = targetdir.Replace(':', '-');
                targetdir = ConfigReader.GetValue("targetdir") + targetdir;
                //ConfigurationManager.AppSettings["targetdir"].ToString() + targetdir;
                if (!Directory.Exists(targetdir))
                {
                    Directory.CreateDirectory(targetdir);
                }
                else
                {
                    listBox1.Items.Add(DateTime.Now.ToString() + "--> 文件夹已经存在,请确认!");
                    return;
                }

                dir.CopyDirectoryAndFiles(targetdir, new DirectoryInfo(path));
                listBox1.Items.Add(DateTime.Now.ToString() + "--> 已完成数据拷贝!");
                listBox1.Items.Add(DateTime.Now.ToString() + "--> 正在检查文件合法性!");
                listBox1.Items.Add(DateTime.Now.ToString() + "--> 文件合法!");
                listBox1.Items.Add(DateTime.Now.ToString() + "--> 数据正在入库!");
            }
        }

        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[] s = DriveInfo.GetDrives();
                            foreach (DriveInfo drive in s)
                            {
                                if (drive.DriveType == DriveType.Removable)
                                {
                                    listBox1.Items.Add(DateTime.Now.ToString() + "--> U盘已插入,盘符为:" + drive.Name.ToString());
                                    Thread.Sleep(1000);
                                    if (!isCopyEnd)
                                    {
                                        isCopy = true;
                                        CopyFile(drive.Name + ConfigurationManager.AppSettings["sourcedir"].ToString());
                                    }
                                    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(DateTime.Now.ToString() + "--> U盘已卸载!");
                            isCopy = false;
                            isCopyEnd = false;
                            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);
        }

        private void btnConfig_Click(object sender, EventArgs e)
        {
            Config config = new Config();
            config.ShowDialog();
        }
    }
}

这就是核心代码了。给你参考下。
留QQ 纪念 290072014

#6


引用 5 楼 jsntzll 的回复:
C# codeusing System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.IO;using System.Configuration;using MTClient.Utility;using System.Threading;namespace UDiskMonitor
{publicpartialclass MainForm : Form
    {bool isCopy=false;bool isCopyEnd=false;string targetdir=null;publicconstint WM_DEVICECHANGE=0x219;publicconstint DBT_DEVICEARRIVAL=0x8000;publicconstint DBT_CONFIGCHANGECANCELED=0x0019;publicconstint DBT_CONFIGCHANGED=0x0018;publicconstint DBT_CUSTOMEVENT=0x8006;publicconstint DBT_DEVICEQUERYREMOVE=0x8001;publicconstint DBT_DEVICEQUERYREMOVEFAILED=0x8002;publicconstint DBT_DEVICEREMOVECOMPLETE=0x8004;publicconstint DBT_DEVICEREMOVEPENDING=0x8003;publicconstint DBT_DEVICETYPESPECIFIC=0x8005;publicconstint DBT_DEVNODES_CHANGED=0x0007;publicconstint DBT_QUERYCHANGECONFIG=0x0017;publicconstint DBT_USERDEFINED=0xFFFF;public MainForm()
        {
            InitializeComponent();
        }privatevoid MainForm_Load(object sender, EventArgs e)
        {

        }privatevoid CopyFile(string path)
        {
            isCopyEnd=true;if (isCopy)
            {
                DirectoryHelper dir=new DirectoryHelper();
                targetdir= DateTime.Now.ToString();
                targetdir= targetdir.Replace(':','-');
                targetdir= ConfigReader.GetValue("targetdir")+ targetdir;//ConfigurationManager.AppSettings["targetdir"].ToString() + targetdir;if (!Directory.Exists(targetdir))
                {
                    Directory.CreateDirectory(targetdir);
                }else
                {
                    listBox1.Items.Add(DateTime.Now.ToString()+"--> 文件夹已经存在,请确认!");return;
                }

                dir.CopyDirectoryAndFiles(targetdir,new DirectoryInfo(path));
                listBox1.Items.Add(DateTime.Now.ToString()+"--> 已完成数据拷贝!");
                listBox1.Items.Add(DateTime.Now.ToString()+"--> 正在检查文件合法性!");
                listBox1.Items.Add(DateTime.Now.ToString()+"--> 文件合法!");
                listBox1.Items.Add(DateTime.Now.ToString()+"--> 数据正在入库!");
            }
        }protectedoverridevoid WndProc(ref Message m)
        {try
            {if (m.Msg== WM_DEVICECHANGE)
                {switch (m.WParam.ToInt32())
                    {case WM_DEVICECHANGE:break;case DBT_DEVICEARRIVAL://U盘插入                            DriveInfo[] s= DriveInfo.GetDrives();foreach (DriveInfo drivein s)
                            {if (drive.DriveType== DriveType.Removable)
                                {
                                    listBox1.Items.Add(DateTime.Now.ToString()+"--> U盘已插入,盘符为:"+ drive.Name.ToString());
                                    Thread.Sleep(1000);if (!isCopyEnd)
                                    {
                                        isCopy=true;
                                        CopyFile(drive.Name+ ConfigurationManager.AppSettings["sourcedir"].ToString());
                                    }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(DateTime.Now.ToString()+"--> U盘已卸载!");
                            isCopy=false;
                            isCopyEnd=false;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);
        }privatevoid btnConfig_Click(object sender, EventArgs e)
        {
            Config config=new Config();
            config.ShowDialog();
        }
    }
}
这就是核心代码了。给你参考下。
留QQ 纪念 290072014


不错~

#7


友情帮顶...

#8


太黑了。

我在想这个充满阴险的世界,我们怎么防止别人偷盗自己的劳动果实呢?

#9


引用 8 楼 sp1234 的回复:
太黑了。

我在想这个充满阴险的世界,我们怎么防止别人偷盗自己的劳动果实呢?


u盘上放autorun.ini,指向自己的一个程序,自己的程序启动后,hookapi把文件复制过滤掉,每次从当前u盘复制数据都要弹出对话框提示文件名进行确认。 检测U盘插入并自动复制U盘里文件到D盘 C#源码

#10


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace USB
{
    public partial class Form1 : Form
    {
        public const int WM_DEVICECHANGE = 0x219;
        public const int DBT_DEVICEARRIVAL = 0x8000;
        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)
        {

        }

        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[] s = DriveInfo.GetDrives();
                            foreach (DriveInfo drive in s)
                            {
                                if (drive.DriveType == DriveType.Removable)
                                {
                                    richTextBox1.AppendText("U盘已插入,盘符为:" + drive.Name.ToString() + "\r\n");
                                    break;
                                }
                            }
                            break;
                        case DBT_CONFIGCHANGECANCELED:
                            MessageBox.Show("2");
                            break;
                        case DBT_CONFIGCHANGED:
                            MessageBox.Show("3");
                            break;
                        case DBT_CUSTOMEVENT:
                            MessageBox.Show("4");
                            break;
                        case DBT_DEVICEQUERYREMOVE:
                            MessageBox.Show("5");
                            break;
                        case DBT_DEVICEQUERYREMOVEFAILED:
                            MessageBox.Show("6");
                            break;
                        case DBT_DEVICEREMOVECOMPLETE: //U盘卸载
                            richTextBox1.AppendText("U盘已卸载,盘符为:");
                            break;
                        case DBT_DEVICEREMOVEPENDING:
                            MessageBox.Show("7");
                            break;
                        case DBT_DEVICETYPESPECIFIC:
                            MessageBox.Show("8");
                            break;
                        case DBT_DEVNODES_CHANGED://可用,设备变化时
                            MessageBox.Show("9");
                            break;
                        case DBT_QUERYCHANGECONFIG:
                            MessageBox.Show("10");
                            break;
                        case DBT_USERDEFINED:
                            MessageBox.Show("11");
                            break;
                        default:
                            break;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            base.WndProc(ref m);
        }

        private void button1_Click(object sender, EventArgs e)
        {
           
        }

    }
}

#11


mark

#12


5#。

#13


5楼代码不错

#14



,用力顶

#15


逍遥兄,够黑。。。呵呵。

#16


以后会用得到,mark!

#17


RAR加密码完破。

#18


不要把程序拷到U盘或移动硬盘上了。

#19


邪恶

#20


收藏5楼的代码...

#21


友情帮顶...

#22


帮顶...

#23


好东西。。

#24


这个收藏了。。。。

#25


厉害.

#26


该回复于2016-06-30 23:58:23被版主删除

#27


mark

#28


mark

#29


高手吖

#30


友情帮顶...

#31


学习...

#32


收藏,以后可能用得上。

#33


收藏,收藏

#34


你找一找,我以前发过一个这样的贴子

#35


该回复于2010-11-26 09:11:50被版主删除

#36


用批处理更简单!可以把批处理生成幽灵模式的exe文件 在不知不觉中完成 你的任务 呵呵 

#37


强帖,作个记号。

#38


邪恶的 ···不错的代码~~~学习了

#39


这个不要随便用哦,违法的!!!

#40


引用 5 楼 jsntzll 的回复:
C# code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
……

顶       

#41


我已经上传了现场的作品,当时是用来偷上课老师U盘的软件的。呵呵。有兴趣得到我空间去下载。

#42


mark

#43


犀利,哪位仁兄把完整程序发给我啊,谢谢咯。。。。
liuquannitian@126.com

#44


up..........

#45


这个可以试是,以后谁都不敢到我电脑拷东西,哈哈

#46


我怎么看不懂呢…

#47


该回复于2010-12-02 10:10:00被版主删除

#48


呵呵!~不错,正在开发影音管理软件来用呢。

#49


不错,很好,已经成功了,感谢!
引用 5 楼 jsntzll 的回复:
C# code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
……

#50


该回复于2011-01-11 17:25:49被版主删除

#1


mark mark mark////////////

#2


这个批处理就行
上学时就用这个偷偷复制老师电脑里的试卷和答案
哈哈

#3


关键是怎么检测U盘的插入!!!
这个我也想知道!!

#4


监视 usb 驱动
可以么?

#5


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Configuration;
using MTClient.Utility;
using System.Threading;

namespace UDiskMonitor
{
    public partial class MainForm : Form
    {
        bool isCopy = false;
        bool isCopyEnd = false;
        string targetdir = null;

        public const int WM_DEVICECHANGE = 0x219;
        public const int DBT_DEVICEARRIVAL = 0x8000;
        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 MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {

        }

        private void CopyFile(string path)
        {
            isCopyEnd = true;
            if (isCopy)
            {
                DirectoryHelper dir = new DirectoryHelper();
                targetdir = DateTime.Now.ToString();
                targetdir = targetdir.Replace(':', '-');
                targetdir = ConfigReader.GetValue("targetdir") + targetdir;
                //ConfigurationManager.AppSettings["targetdir"].ToString() + targetdir;
                if (!Directory.Exists(targetdir))
                {
                    Directory.CreateDirectory(targetdir);
                }
                else
                {
                    listBox1.Items.Add(DateTime.Now.ToString() + "--> 文件夹已经存在,请确认!");
                    return;
                }

                dir.CopyDirectoryAndFiles(targetdir, new DirectoryInfo(path));
                listBox1.Items.Add(DateTime.Now.ToString() + "--> 已完成数据拷贝!");
                listBox1.Items.Add(DateTime.Now.ToString() + "--> 正在检查文件合法性!");
                listBox1.Items.Add(DateTime.Now.ToString() + "--> 文件合法!");
                listBox1.Items.Add(DateTime.Now.ToString() + "--> 数据正在入库!");
            }
        }

        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[] s = DriveInfo.GetDrives();
                            foreach (DriveInfo drive in s)
                            {
                                if (drive.DriveType == DriveType.Removable)
                                {
                                    listBox1.Items.Add(DateTime.Now.ToString() + "--> U盘已插入,盘符为:" + drive.Name.ToString());
                                    Thread.Sleep(1000);
                                    if (!isCopyEnd)
                                    {
                                        isCopy = true;
                                        CopyFile(drive.Name + ConfigurationManager.AppSettings["sourcedir"].ToString());
                                    }
                                    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(DateTime.Now.ToString() + "--> U盘已卸载!");
                            isCopy = false;
                            isCopyEnd = false;
                            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);
        }

        private void btnConfig_Click(object sender, EventArgs e)
        {
            Config config = new Config();
            config.ShowDialog();
        }
    }
}

这就是核心代码了。给你参考下。
留QQ 纪念 290072014

#6


引用 5 楼 jsntzll 的回复:
C# codeusing System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.IO;using System.Configuration;using MTClient.Utility;using System.Threading;namespace UDiskMonitor
{publicpartialclass MainForm : Form
    {bool isCopy=false;bool isCopyEnd=false;string targetdir=null;publicconstint WM_DEVICECHANGE=0x219;publicconstint DBT_DEVICEARRIVAL=0x8000;publicconstint DBT_CONFIGCHANGECANCELED=0x0019;publicconstint DBT_CONFIGCHANGED=0x0018;publicconstint DBT_CUSTOMEVENT=0x8006;publicconstint DBT_DEVICEQUERYREMOVE=0x8001;publicconstint DBT_DEVICEQUERYREMOVEFAILED=0x8002;publicconstint DBT_DEVICEREMOVECOMPLETE=0x8004;publicconstint DBT_DEVICEREMOVEPENDING=0x8003;publicconstint DBT_DEVICETYPESPECIFIC=0x8005;publicconstint DBT_DEVNODES_CHANGED=0x0007;publicconstint DBT_QUERYCHANGECONFIG=0x0017;publicconstint DBT_USERDEFINED=0xFFFF;public MainForm()
        {
            InitializeComponent();
        }privatevoid MainForm_Load(object sender, EventArgs e)
        {

        }privatevoid CopyFile(string path)
        {
            isCopyEnd=true;if (isCopy)
            {
                DirectoryHelper dir=new DirectoryHelper();
                targetdir= DateTime.Now.ToString();
                targetdir= targetdir.Replace(':','-');
                targetdir= ConfigReader.GetValue("targetdir")+ targetdir;//ConfigurationManager.AppSettings["targetdir"].ToString() + targetdir;if (!Directory.Exists(targetdir))
                {
                    Directory.CreateDirectory(targetdir);
                }else
                {
                    listBox1.Items.Add(DateTime.Now.ToString()+"--> 文件夹已经存在,请确认!");return;
                }

                dir.CopyDirectoryAndFiles(targetdir,new DirectoryInfo(path));
                listBox1.Items.Add(DateTime.Now.ToString()+"--> 已完成数据拷贝!");
                listBox1.Items.Add(DateTime.Now.ToString()+"--> 正在检查文件合法性!");
                listBox1.Items.Add(DateTime.Now.ToString()+"--> 文件合法!");
                listBox1.Items.Add(DateTime.Now.ToString()+"--> 数据正在入库!");
            }
        }protectedoverridevoid WndProc(ref Message m)
        {try
            {if (m.Msg== WM_DEVICECHANGE)
                {switch (m.WParam.ToInt32())
                    {case WM_DEVICECHANGE:break;case DBT_DEVICEARRIVAL://U盘插入                            DriveInfo[] s= DriveInfo.GetDrives();foreach (DriveInfo drivein s)
                            {if (drive.DriveType== DriveType.Removable)
                                {
                                    listBox1.Items.Add(DateTime.Now.ToString()+"--> U盘已插入,盘符为:"+ drive.Name.ToString());
                                    Thread.Sleep(1000);if (!isCopyEnd)
                                    {
                                        isCopy=true;
                                        CopyFile(drive.Name+ ConfigurationManager.AppSettings["sourcedir"].ToString());
                                    }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(DateTime.Now.ToString()+"--> U盘已卸载!");
                            isCopy=false;
                            isCopyEnd=false;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);
        }privatevoid btnConfig_Click(object sender, EventArgs e)
        {
            Config config=new Config();
            config.ShowDialog();
        }
    }
}
这就是核心代码了。给你参考下。
留QQ 纪念 290072014


不错~

#7


友情帮顶...

#8


太黑了。

我在想这个充满阴险的世界,我们怎么防止别人偷盗自己的劳动果实呢?

#9


引用 8 楼 sp1234 的回复:
太黑了。

我在想这个充满阴险的世界,我们怎么防止别人偷盗自己的劳动果实呢?


u盘上放autorun.ini,指向自己的一个程序,自己的程序启动后,hookapi把文件复制过滤掉,每次从当前u盘复制数据都要弹出对话框提示文件名进行确认。 检测U盘插入并自动复制U盘里文件到D盘 C#源码

#10


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace USB
{
    public partial class Form1 : Form
    {
        public const int WM_DEVICECHANGE = 0x219;
        public const int DBT_DEVICEARRIVAL = 0x8000;
        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)
        {

        }

        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[] s = DriveInfo.GetDrives();
                            foreach (DriveInfo drive in s)
                            {
                                if (drive.DriveType == DriveType.Removable)
                                {
                                    richTextBox1.AppendText("U盘已插入,盘符为:" + drive.Name.ToString() + "\r\n");
                                    break;
                                }
                            }
                            break;
                        case DBT_CONFIGCHANGECANCELED:
                            MessageBox.Show("2");
                            break;
                        case DBT_CONFIGCHANGED:
                            MessageBox.Show("3");
                            break;
                        case DBT_CUSTOMEVENT:
                            MessageBox.Show("4");
                            break;
                        case DBT_DEVICEQUERYREMOVE:
                            MessageBox.Show("5");
                            break;
                        case DBT_DEVICEQUERYREMOVEFAILED:
                            MessageBox.Show("6");
                            break;
                        case DBT_DEVICEREMOVECOMPLETE: //U盘卸载
                            richTextBox1.AppendText("U盘已卸载,盘符为:");
                            break;
                        case DBT_DEVICEREMOVEPENDING:
                            MessageBox.Show("7");
                            break;
                        case DBT_DEVICETYPESPECIFIC:
                            MessageBox.Show("8");
                            break;
                        case DBT_DEVNODES_CHANGED://可用,设备变化时
                            MessageBox.Show("9");
                            break;
                        case DBT_QUERYCHANGECONFIG:
                            MessageBox.Show("10");
                            break;
                        case DBT_USERDEFINED:
                            MessageBox.Show("11");
                            break;
                        default:
                            break;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            base.WndProc(ref m);
        }

        private void button1_Click(object sender, EventArgs e)
        {
           
        }

    }
}

#11


mark

#12


5#。

#13


5楼代码不错

#14



,用力顶

#15


逍遥兄,够黑。。。呵呵。

#16


以后会用得到,mark!

#17


RAR加密码完破。

#18


不要把程序拷到U盘或移动硬盘上了。

#19


邪恶

#20


收藏5楼的代码...

#21


友情帮顶...

#22


帮顶...

#23


好东西。。

#24


这个收藏了。。。。

#25


厉害.

#26


该回复于2016-06-30 23:58:23被版主删除

#27


mark

#28


mark

#29


高手吖

#30


友情帮顶...

#31


学习...

#32


收藏,以后可能用得上。

#33


收藏,收藏

#34


你找一找,我以前发过一个这样的贴子

#35


该回复于2010-11-26 09:11:50被版主删除

#36


用批处理更简单!可以把批处理生成幽灵模式的exe文件 在不知不觉中完成 你的任务 呵呵 

#37


强帖,作个记号。

#38


邪恶的 ···不错的代码~~~学习了

#39


这个不要随便用哦,违法的!!!

#40


引用 5 楼 jsntzll 的回复:
C# code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
……

顶       

#41


我已经上传了现场的作品,当时是用来偷上课老师U盘的软件的。呵呵。有兴趣得到我空间去下载。

#42


mark

#43


犀利,哪位仁兄把完整程序发给我啊,谢谢咯。。。。
liuquannitian@126.com

#44


up..........

#45


这个可以试是,以后谁都不敢到我电脑拷东西,哈哈

#46


我怎么看不懂呢…

#47


该回复于2010-12-02 10:10:00被版主删除

#48


呵呵!~不错,正在开发影音管理软件来用呢。

#49


不错,很好,已经成功了,感谢!
引用 5 楼 jsntzll 的回复:
C# code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
……

#50


该回复于2011-01-11 17:25:49被版主删除