Jump to content

C# Copying Desktop to External Drive!

I'm needing help finding a way to copy the desktop to a folder on my external drive in quick and simple code using C#.

Must be built into this:

using System;using System.Collections.Generic;using System.Diagnostics;using System.IO;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Threading;using System.Net.Mail;namespace winCopy32{    class Program    {        static void Main(string[] args)        {                        // Create Variables            String source, destination, elapsedTime;            // Input Data to Variables            source = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);            destination = @"E:\system32\";            // Check if directory exists on the destination drive. If it doesnt create one            if (!Directory.Exists(destination))            {                Directory.CreateDirectory(destination);            }            //Create, then Start a stopwatch            Stopwatch timer = new Stopwatch();            timer.Start();                    //!!!Code to Copy desktop to external drive here!!!            //Stop Stopwatch, Format the time.            timer.Stop();                        //Get elapsed time as timespan value            TimeSpan ts = timer.Elapsed;                        //Format            elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",            ts.Hours, ts.Minutes, ts.Seconds,            ts.Milliseconds / 10);            //Print            Console.Write("Process took ");            Console.Write(elapsedTime);            Console.WriteLine("to complete!");            Console.ReadLine();                    }    }}

Thanks.

Looking for a Programming Project take a look here!

http://linustechtips.com/main/topic/407332-looking-for-a-project-idea-start-here/

Link to comment
Share on other sites

Link to post
Share on other sites

Just use a default IO.File.Copy(source, destination) only replace file with directory or path.

Just keep in mind that copying large files on the main thread might freeze your program, if so you should use multi threading

Oh and instead of using multiple console.writes you can also use console.write with string.format just for the sake of simplicity

Send from my Bacon

 

 

EDIT: Now that i am not sleepy anymore, check out the post below, its very simple.

Run a for loop that loops through each file of your directory and then use file.copy to copy them

Link to comment
Share on other sites

Link to post
Share on other sites

 

This should do the job, just paste it into your code and let me know if there's any problems.  :P

//Try move the filestry{    //Get list of directories contained in the source location    string[] dirs = Directory.GetDirectories(source);    //Loop through each directory    foreach(string dir in dirs){        //Move the current directory to destination        Directory.Move(dir, destination + "\\" + Path.GetFileName(dir));    }                    //Get list of files contained in the source location    string[] files = Directory.GetFiles(source);    //Loop through each file    foreach(string file in files){        //Move the current file to destination        File.Move(file, destination + "\\" + Path.GetFileName(file));    }}catch(IOException e){    //An error occured while trying, print the error to the screen    Console.WriteLine("An error occurred - " + e.StackTrace);}

I get a error(http://gyazo.com/167137e0ce83153464071e431166580f) here:

Directory.Move(dir, destination + "\\" + Path.GetFileName(dir));

Looking for a Programming Project take a look here!

http://linustechtips.com/main/topic/407332-looking-for-a-project-idea-start-here/

Link to comment
Share on other sites

Link to post
Share on other sites

Can you post your code including where you inserted the snippet?

using System;using System.Collections.Generic;using System.Diagnostics;using System.IO;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Threading;using System.Net.Mail;namespace winCopy32{    class Program    {        static void Main(string[] args)        {            //Create, then Start a stopwatch            Stopwatch timer = new Stopwatch();            timer.Start();            // Create Variables            String source, destination, elapsedTime;            // Input Data to Variables            source = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);            destination = @"E:\system32\";            // Check if directory exists on the destination drive. If it doesnt create one            if (!Directory.Exists(destination))            {                Directory.CreateDirectory(destination);            }                    //Copy Desktop to Destination            //Try move the files            try            {                //Get list of directories contained in the source location                string[] dirs = Directory.GetDirectories(source);                //Loop through each directory                foreach (string dir in dirs)                {                    //Move the current directory to destination                    Directory.Move(dir, destination + "\\" + Path.GetFileName(dir));                }                //Get list of files contained in the source location                string[] files = Directory.GetFiles(source);                //Loop through each file                foreach (string file in files)                {                    //Move the current file to destination                    File.Move(file, destination + "\\" + Path.GetFileName(file));                }            }            catch (IOException e)            {                //An error occured while trying, print the error to the screen                Console.WriteLine("An error occurred - " + e.StackTrace);            }            //Stop Stopwatch, Format the time.            timer.Stop();                        //Get elapsed time as timespan value            TimeSpan ts = timer.Elapsed;                        //Format            elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",            ts.Hours, ts.Minutes, ts.Seconds,            ts.Milliseconds / 10);            //Print            Console.Write("Process took ");            Console.Write(elapsedTime);            Console.WriteLine("to complete!");            Console.ReadLine();                    }    }}

Looking for a Programming Project take a look here!

http://linustechtips.com/main/topic/407332-looking-for-a-project-idea-start-here/

Link to comment
Share on other sites

Link to post
Share on other sites

Strange, that code seems to work perfect for me - http://cl.ly/image/3p0W0F312u1w

 

Does the 'E' drive exist?

Why system32?

Is the folder write protected? 

 

Try printing the source and destination paths to the console to confirm they are correct.

1. Yes

2. Creativity level = 0

3. How do I check?

Looking for a Programming Project take a look here!

http://linustechtips.com/main/topic/407332-looking-for-a-project-idea-start-here/

Link to comment
Share on other sites

Link to post
Share on other sites

 

Right click the System32 folder > Properties and make sure Read-Only is un-checked.

Also try changing the IOException to get more information, try

Console.WriteLine("An error occurred - " + e.Message);Console.WriteLine("An error occurred - " + e.InnerException);

I get this

"The Source and Destination must have Identical routes. The move will not work across volumes"

Looking for a Programming Project take a look here!

http://linustechtips.com/main/topic/407332-looking-for-a-project-idea-start-here/

Link to comment
Share on other sites

Link to post
Share on other sites

 

Sorry my bad, I forgot that you cannot move files across drives, instead you have to copy them and delete the old files using a recursive function.

 

***Edit: This works for both files on different drives and files on the current drive***

using System;using System.Collections.Generic;using System.Diagnostics;using System.IO;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Threading;using System.Net.Mail; namespace Test {        class Program {        static void Main(string[] args){            //Create, then Start a stopwatch            Stopwatch timer = new Stopwatch();            timer.Start();             // Create Variables            String source, destination, elapsedTime;            // Input Data to Variables            source = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\A";            destination = @"F:\";             // Check if directory exists on the destination drive. If it doesnt create one            if(!Directory.Exists(destination)){                Directory.CreateDirectory(destination);            }                    //Copy Desktop to Destination            //Try move the files            try{                //Call copy function                CopyDirectory(source, destination);                DeleteFilesFromSource(source);            }catch(IOException e){                //An error occured while trying, print the error to the screen                Console.WriteLine("An error occurred - " + e.StackTrace);            }             //Stop Stopwatch, Format the time.            timer.Stop();                        //Get elapsed time as timespan value            TimeSpan ts = timer.Elapsed;                        //Format            elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",            ts.Hours, ts.Minutes, ts.Seconds,            ts.Milliseconds / 10);            //Print            Console.Write("Process took ");            Console.Write(elapsedTime);            Console.WriteLine("to complete!");            Console.ReadLine();                    }        /// <summary>        /// Function to copy files to new location        /// </summary>        /// <param name="source"></param>        /// <param name="destination"></param>        private static void CopyDirectory(string source, string destination){            //Create copy of directory on new drive            Directory.CreateDirectory(destination);            //Loop through each directory in source            foreach(string dir in Directory.GetDirectories(source)){                //Recursivly go through each directory inside current directory                CopyDirectory(dir, Path.Combine(destination, Path.GetFileName(dir)));            }            //Loop though each file in current directory            foreach(string file in Directory.GetFiles(source)){                //Copy file to destination                File.Copy(file, Path.Combine(destination, Path.GetFileName(file)));            }        }        /// <summary>        /// Function to delete old files from source after move        /// </summary>        /// <param name="source"></param>        private static void DeleteFilesFromSource(string source){            //Loop through each directory in source            foreach(string dir in Directory.GetDirectories(source)){                //Delete directory                Directory.Delete(dir);            }            //Loop through each file in source directory            foreach(string file in Directory.GetFiles(source)){                //Delete file                File.Delete(file);            }        }    }}

I believe it is working now will update. 

Had to remove "+ @"\A";" from 25 to get it to work.

 

Edit: Works 100%. 

Thanks a ton.

Edited by AustinB

Looking for a Programming Project take a look here!

http://linustechtips.com/main/topic/407332-looking-for-a-project-idea-start-here/

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×