Click here to Skip to main content
15,884,629 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am having a requirement where I want to copy files from one folder to another where I should create a destination folder programatically and copy files in it and I want to do it from app.config file and don't want to specify the paths directly. As I was new to coding I don't know how to do it. Please any body tell me how to do this.


Thanks in advance.
Posted
Updated 14-May-14 18:54pm
v2
Comments
Prasad Avunoori 15-May-14 1:02am    
Are source and destination file paths mentioned in App.config?
David_Wimbley 15-May-14 1:31am    
If you don't want to specify the file paths directly then how else are you going to tell your app where to put the files???? Am i missing something?
Karen Mitchelle 15-May-14 1:39am    
Where are you going to transfer the files and/or where are you going to copy it? Is it from a server or just from a local folder?
Member 10042156 15-May-14 3:06am    
from local folder

1 solution

C#
private void CopyFiles()
   {

       string sourcePath = ConfigurationSettings.AppSettings["sourceFolder"].ToString();

       string destinationPath=  ConfigurationSettings.AppSettings["DestinationFolder"].ToString();

       if(!Directory.Exists(destinationPath))
       {
           Directory.CreateDirectory(destinationPath);
       }

       DirectoryInfo d = new DirectoryInfo(sourcePath);

       foreach (var file in d.GetFiles())
       {
             Directory.Move(file.FullName, destinationPath +"\\"+ file.Name);
       }

   }


XML
<configuration>
  <appSettings>
    <add key="sourceFolder" value="C:\Documents and Settings\aavprasad\Desktop\Source"/>
    <add key="DestinationFolder" value="C:\Documents and Settings\aavprasad\Desktop\Destination"/>
  </appSettings>
</configuration>
 
Share this answer
 
v2

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