Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:

C#
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

You could use File.Copy[^]

C#
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK)
    {
    string src = ofd.FileName;
    string dst = src.Replace("C:\\", "D:\\");
    File.Copy(src, dst, true);
    }
 
Share this answer
 
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.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900