想要在将USB插入计算机时自动从PC复制数据 [英] Want to copy data automatically from pc when plug usb into

查看:94
本文介绍了想要在将USB插入计算机时自动从PC复制数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I want to plug usb into the pc will automatically copy the selected folder to usb, but have trouble:
Plug in usb 1 to pc then ok.
plug usb 2 into pc then usb 1 stop copy, when usb 2 copy finished, usb 1 continues.
Now I want to plug in multiple usb at the same time all are copy.



我尝试过的事情:



What I have tried:

private void OnDriveArrived(object sender, DriveDetectorEventArgs e)
       {

               string s = "Phát hiện ổ " + e.Drive + " Đã cắm vào";
               listBox1.Items.Add(s);

               string copy1 = textBox1.Text;
           try
           {

                   FileSystem.CopyDirectory(copy1, e.Drive, UIOption.AllDialogs, UICancelOption.DoNothing);


           }
           catch (Exception ex)
           {
               MessageBox.Show("Error : Kiểm tra đường dẩn mục copy",
                               "Error",
                               MessageBoxButtons.OK,
                               MessageBoxIcon.Error);
           }
           return;

       }

推荐答案

报价:

现在我要插入多个USB同时全部被复制.

Now I want to plug in multiple usb at the same time all are copy.


答案是多线程.
主线程包含usb侦听器,插入后,您将为每个usb密钥启动一个复制线程.


The answer is multithreading.
The main thread contain the usb listener and you launch a copy thread for each usb key as they are plugged.


private void OnDriveArrived(object sender, DriveDetectorEventArgs e)
        {
           
               
                string s = "Phát hiện ổ " + e.Drive + " Đã cắm vào";
          
                listBox1.Items.Add(s);
           Thread thread = new Thread(() =>
            {
                try
                {
                    string copy1 = textBox1.Text;
                    FileSystem.CopyDirectory(
                            copy1,
                            e.Drive,
                            UIOption.AllDialogs,
                            UICancelOption.DoNothing);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error : " + ex.Message,
                                    "Error",
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Error);
                }
            });
            thread.Start();
            
           
        }


这篇关于想要在将USB插入计算机时自动从PC复制数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆