Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Windows clipboard USB , +
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);
            }
        }
    }
}
 

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
Posted 30-Nov-12 22:34pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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);
    }
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Maciej Los 315
1 OriginalGriff 233
2 Aajmot Sk 224
3 Richard MacCutchan 220
4 Marcin Kozub 210
0 OriginalGriff 7,853
1 Sergey Alexandrovich Kryukov 7,107
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,790


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 1 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100