5 Replies - 35103 Views - Last Post: 27 June 2008 - 04:37 AM Rate Topic: -----

#1 Sentry68   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 25-June 08

Copy multiple files

Post icon  Posted 25 June 2008 - 03:07 PM

Hello all,

My name's Bill, I'm a complete noob beginner looking for a home! I'm working in Visual Studio 2008 using C#. I have several programs I want to build as I get my skill level up, but I seem to keep getting hung up on simple stuff, so I'm going to have to start asking questions. I hope this is a good site for beginners...

My question is a simple one- I'm trying to create an event when a button is clicked to copy all files from one location to another- eventually, from a flash drive to the C: drive of a computer, but I'm just copying from a test directory for now. Here's the basic code for the button_Click event that I'm trying to make work....

		private void button2_Click(object sender, EventArgs e)
		{
			String[] files=Directory.GetFiles("C:\\test\\source", "*.*");

			foreach(string file in files)
			{
				string destfile = Path.Combine("C:\\test\\destination", file);
				File.Copy (file, destfile, true);
				
			}

		}


I know it will need a Try Catch and etc., but right now I just want to make it work. I can run the Form, but when I click the button I get an unhandled exception, saying it cannot access the file because it is in use by another process. If I change the destination to an absolute file name it works, but I only end up with one file in the destination folder because it's set to overwrite and it just copies each file, overwriting the last with the same name. I'm guessing somehow it's locking the file during copy and can't retrieve the file name to use in the destination folder or something I don't know. Anyways, I've written it several ways, and tried several snippets I've found all around and can't find a working solution. TIA for any help, and please don't assume I know what I'm doing at all!

This post has been edited by Sentry68: 25 June 2008 - 03:10 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Copy multiple files

#2 zakary   User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • Posts: 427
  • Joined: 15-February 05

Re: Copy multiple files

Posted 25 June 2008 - 05:33 PM

cannot access the file because it is in use by another process. this exception is happening because one of the files in C:\\test\\source is open by its program or the first time you ran this App. it opened one of the files and your app still has a handle running you your Task Manager.
Was This Post Helpful? 0
  • +
  • -

#3 Sentry68   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 25-June 08

Re: Copy multiple files

Posted 26 June 2008 - 06:35 AM

Hi, thanks for the response.

I can't find anything that would have the file open. It's a jpg image, it's not open in any viewer, and no explorer windows are open. The app has no code to view or open the image, just copy files. The exception shows me which file, it's the first in the folder. If I move that file out of the source folder and run it again, the same thing happens with the next file. If I give an absolute file name to copy it to, it works so I think it has to be a problem with where I'm trying to pick up the file's original name and give it to the file copy in the new location. If I use this line:
 File.Copy (file, "C:\\test\\destination\\thefile.jpg", true);


it will copy. But since it's multiple files, it goes through the array copying each one with the same name and overwriting the previous. I need it to use each file's original name.... don't know how to fix.
Was This Post Helpful? 0
  • +
  • -

#4 zakary   User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • Posts: 427
  • Joined: 15-February 05

Re: Copy multiple files

Posted 26 June 2008 - 12:31 PM

// where you have "*.*" and the correct way is "*"
string[] files = System.IO.Directory.GetFiles(@"C:\Development\HumanHunter", "*");
                        
            foreach (string file in files)
            {
                // here we set the destination string with the file name
                string destfile = @"C:\test\destination\" +  System.IO.Path.GetFileName(file); 
                // now we copy the file to that destination
                System.IO.File.Copy(file, destfile, true);
            }


Was This Post Helpful? 1
  • +
  • -

#5 Sentry68   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 25-June 08

Re: Copy multiple files

Posted 26 June 2008 - 01:20 PM

Excellent, thank you Zakary. Works great. I noticed one interesting thing: when I first changed it I forgot to put a trailing \ in the destfile string so it copied the files into c:\test and appended 'destination' to the file name, so the file was called destinationfilename.jpg. Unexpected, but interesting to know.

I had tried a couple ways to append the file name with the + sign and kept getting different errors, but your code worked great. I think I was using () wrong or something. Thanks again!
Was This Post Helpful? 0
  • +
  • -

#6 zakary   User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • Posts: 427
  • Joined: 15-February 05

Re: Copy multiple files

Posted 27 June 2008 - 04:37 AM

right the \ needs to be in the string because th + appends one string to another so if \ is missing you will get destinationfilename.jpg. I am glad I could help and make sure u use a try catch and catch FileNotFoundException.

Feel free to come back if you need anymore help, and let your friends know about <D.I.C>
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1