C#实现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.IO;


namespace usbtest
{
    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;

        string destPath = null;
        string sourcePath = @"G:\tmp";
        string fileName = "wdxx.zip";

        public Form1()
        {
            InitializeComponent();
        }

        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)
                                {
                                    destPath = drive.Name.ToString();
                                    label1.Text = "U盘已插入,盘符为:" + destPath;
                                    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盘卸载 
                            label1.Text = "";
                            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 button1_Click(object sender, EventArgs e)
        {
            try
            {
                File.Copy(System.IO.Path.Combine(sourcePath, fileName), System.IO.Path.Combine(destPath, fileName));
                MessageBox.Show("文件写入成功!");
            }
            catch (IOException copyError)
            {
                MessageBox.Show(copyError.Message);
                return;
            }
        }
    }
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个使用C#多线程ping并将结果写入文件的示例代码: ```csharp using System; using System.Collections.Generic; using System.IO; using System.Net; using System.Net.NetworkInformation; using System.Threading; class Program { static void Main(string[] args) { // 创建一个列表,包含要ping的IP地址 List<string> addresses = new List<string> { "www.google.com", "www.baidu.com", "www.microsoft.com", "www.apple.com", "www.amazon.com" }; // 创建一个文件流,用于写入ping结果 using (StreamWriter writer = new StreamWriter("ping_results.txt")) { // 创建一个计数器,用于跟踪已完成ping的地址数量 int count = 0; // 遍历IP地址列表 foreach (string address in addresses) { // 创建一个新线程执行ping操作 Thread t = new Thread(() => { try { // 创建一个Ping对象并发送ping请求 Ping pingSender = new Ping(); PingReply reply = pingSender.Send(address); // 将ping结果写入文件 writer.WriteLine("{0}: {1}", address, reply.Status); } catch (Exception ex) { // 发生异常时也将结果写入文件 writer.WriteLine("{0}: {1}", address, ex.Message); } finally { // 计数器加1 Interlocked.Increment(ref count); } }); // 启动线程 t.Start(); } // 等待所有线程完成 while (count < addresses.Count) { Thread.Sleep(100); } } Console.WriteLine("Ping完成,结果已写入文件。"); Console.ReadKey(); } } ``` 在此示例中,我们使用了一个`List<string>`对象来存储要ping的IP地址。我们遍历该列表,并为每个地址创建一个新线程执行ping操作。每次ping完成后,我们将结果写入一个文件中。 注意,我们在写入文件时使用了`StreamWriter`类,它会自动处理文件流的打开和关闭。我们还使用了`Interlocked.Increment`方法来对计数器进行原子操作,以确保线程安全。最后,我们在主线程中等待所有线程完成后输出一个完成消息。 当然,这只是一个简单的示例,你可以根据自己的需求对代码进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值