有没有办法将文件从剪贴板粘贴到USB设备? [英] Is there a way to paste a file from the clipboard to a USB device?

查看:124
本文介绍了有没有办法将文件从剪贴板粘贴到USB设备?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我正在尝试制作一个应用程序,当我将其插入时自动更新USB。一切都很好,我的程序可以检测到当我插入它时,我的USB驱动器和消息框提示用户是否希望更新它。



继承我程序的内部工作原理。用户可以单击一个按钮,弹出一个文件浏览器对话框。用户可以选择他们希望复制的目录并将其传输到USB。当他们选择文件时,文件的路径将添加到核对表框中。因此,他们可以拥有多个目录,他们希望在插入USB时保持更新。



现在出现了问题,由于某些原因,我无法让我的程序将目录从我的电脑复制并粘贴到USB驱动器上。



我尝试使用File.move,因为该类只能在文件而不是目录之间使用。



我还尝试过directory.move,但是我收到一条错误,指出不可能在不同卷之间传输目录。



所以我最后的尝试是尝试使用剪贴板,我能够以编程方式将文件复制到剪贴板,但我找不到将其粘贴到我的USB中的方法,除非我按下CTRL + V,但这对我来说很邋。



这里是我的源代码:



Hi all,

I am trying to make an app that automatically updates my USB when I plug it in. All is well, I got my program to detect my usb drive when I plug it in and a messagebox prompts the user if they wish to update it.

Heres the inner workings of my program. There is a button that the user can click on and a filebrowser dialog pops up. The user can choose their directories that they wish to copy and be transferred to the usb. When they choose the file the path of the file is added to a checklistbox. So, they are able to have multiple directories that they wish to keep updated when they plug in the usb.

Now the problem, For some reason I cant get my program to copy and paste the directories from my computer to the usb drive.

I tried using File.move which it can''t because that class can only be used between files and not directories.

I also tried directory.move but I get an error which states that it is not possible to transfer directories between different volumes.

So my last attempt was to try to use the clipboard, I am able to copy the files programmatically to the clipboard but I cant find a way to paste it into my usb unless I keystroke CTRL+V but that is sloppy to me.

here is my sourcecode:

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.Collections.Specialized;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }



        private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Interval = 1000;
            timer1.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            foreach (DriveInfo drive in DriveInfo.GetDrives())
            {
                if (drive.DriveType == DriveType.Removable)
                {
                    timer1.Stop();



                    if (MessageBox.Show("A USB storage device has been detected, would you like to update it's files? " + drive.Name, "Drive Update Notice", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {

                        foreach (StringCollection listings in checkedListBox1.CheckedItems)
                        {
                            Clipboard.SetFileDropList(listings);





                        }
                    }
                }
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                checkedListBox1.Items.Add(folderBrowserDialog1.SelectedPath);
            }
        }
    }
}







如果您有任何其他方式这样做我想知道,我当然会感谢我收到的所有帮助。



谢谢,



Angel Mendez




If you have any other way of doing this I would like to know and I will of course appreciate all the help I receive.

Thank you,

Angel Mendez

推荐答案

你可以使用 File.Copy [ ^ ]



You could use File.Copy[^]

OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK)
    {
    string src = ofd.FileName;
    string dst = src.Replace("C:\\", "D:\\");
    File.Copy(src, dst, true);
    }


这些MSDN示例应该会有所帮助:如何:复制,删除和移动文件和文件夹(C#编程指南) [ ^ ]。



想法是通过首先创建来递归复制文件夹目的地的文件夹(如果需要)然后将源文件夹中的所有文件复制到目的地。
These MSDN example should be helpful: How to: Copy, Delete, and Move Files and Folders (C# Programming Guide)[^].

The idea is to recursively copy the folder, by first creating the folder at the destination (if needed) and then copy all the files in the source folder to the destination.


这篇关于有没有办法将文件从剪贴板粘贴到USB设备?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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