以编程方式复制文件 [英] copying files progrmatically

查看:70
本文介绍了以编程方式复制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有一个要求,我想将文件从一个文件夹复制到另一个文件夹,我应该以编程方式创建目标文件夹并在其中复制文件我想从app.config文件中执行此操作,并且不希望直接指定路径。由于我刚接触编码,我不知道该怎么做。请任何人告诉我怎么做。





提前致谢。

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.

推荐答案

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);
       }

   }







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


这篇关于以编程方式复制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆