Click here to Skip to main content
15,881,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all
i have to copy an image file from one folder to another folder

C#
if (isPath.Contains(@"\"))
                {
                    string path = System.Web.Configuration.WebConfigurationManager.AppSettings["ImgPath"].ToString();
                    DirectoryInfo di = new DirectoryInfo(path+@"\"+isPath);
                    DirectoryInfo di1 = new DirectoryInfo(path+@"\Bilder\");
                    var directories = di.GetFiles("*", SearchOption.AllDirectories);
                    foreach (FileInfo d in directories)
                    {
                        if (d.Extension == ".jpg")//Bilder
                        {
                            CreateFolder("Bilder");
                           // File.Open(d.FullName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
                            File.OpenRead(d.FullName);
                           // File.OpenWrite(@"D:\Projects\EFO\Project_Synchronizer\Synchronizer WEB\otraWebService\otraWebService\Bilder\");
                           // File.Open(di1.FullName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
                            System.IO.File.Copy(d.FullName, @"D:\Projects\EFO\Project_Synchronizer\Synchronizer WEB\otraWebService\otraWebService\Bilder");
                            //File.Copy(
                        }
                        else if (d.Extension == ".xml")//EFOEksport
                        {
                            CreateFolder("EFOEksport");
                            System.IO.File.Copy(d.FullName, d.Directory.Name, false); 
                        }
                        else
                        {
                            //Nothing
                        }
                    }


it throws an exception
could not find a part of path

plz help [Removed urgency]
Posted
Updated 26-Jun-12 23:42pm
v3
Comments
krumia 27-Jun-12 5:53am    
What does it throw? What kind of exception? Does it have a description? Have you debugged the code and found out which line causes the exception? Without answer to these questions, nobody will be able to answer you I'm afraid.

File.Copy require absolute path("C:\Users\Desktop\testFolder"). when the path given to File.Copy is relative(/testfolder), it will throw an exception "could not find a part of path".

In your case just make sure that the path given is absolute. This will fix your issue.
 
Share this answer
 
v2
Comments
[no name] 27-Jun-12 6:00am    
Debug your application and make sure the path is not relative.
I see a couple of issues...

1) File.OpenRead returns as System.IO.FileStream and now has a lock on that file until the stream is closed, you will receive an exception about the file is in use by another process. I do not see you reading the file so ommit that.

2) The System.IO.File.Copy needs to have a file name for both parameters, you cannot use a destination directory you must add the destination filename as well
e.g.
C#
System.IO.File.Copy(d.FullName, @"D:\Projects\EFO\Project_Synchronizer\Synchronizer WEB\otraWebService\otraWebService\Bilder\" + d.Name;);



C#
foreach (FileInfo d in directories)
                   {
                       if (d.Extension == ".jpg")//Bilder
                       {
                           CreateFolder("Bilder");
                                                     File.OpenRead(d.FullName); 
                           //you may want to check if file exists at destination already and implement action based on that (overwrite or rename with numeric copy value file(Copy 1).ext etc. just a suggestion to avoid exceptions in future                          
                           System.IO.File.Copy(d.FullName, @"D:\Projects\EFO\Project_Synchronizer\Synchronizer WEB\otraWebService\otraWebService\Bilder\" + d.Name);
                           
                       }
 
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