Copy file to a different directory

前端 未结 7 1896
暖寄归人
暖寄归人 2021-02-05 01:23

I am working on a project where I want to copy some files in one directory to a second already existing directory.

I can\'t find a way to simply copy from one folder to

7条回答
  •  北恋
    北恋 (楼主)
    2021-02-05 01:57

    I used this code and it work for me

    //I declare first my variables
    string sourcePath = @"Z:\SourceLocation";
    string targetPath = @"Z:\TargetLocation";
    
    string destFile = Path.Combine(targetPath, fileName);
    string sourceFile = Path.Combine(sourcePath, fileName);
    
    // To copy a folder's contents to a new location:
    // Create a new target folder, if necessary.
    if (!Directory.Exists(targetPath))
    {
        Directory.CreateDirectory(targetPath);
    }
    
    // To copy a file to another location and 
    // overwrite the destination file if it already exists.
    File.Copy(sourceFile, destFile, true);
    

提交回复
热议问题