Copy file to a different directory

前端 未结 7 1895
暖寄归人
暖寄归人 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:40

    This worked for me:

        string picturesFile = @"D:\pictures";
        string destFile = @"C:\Temp\tempFolder\";
    
        string[] files = Directory.GetFiles(picturesFile);
        foreach (var item in files)
        {
           File.Copy(item, destFile + Path.GetFileName(item));
        }
    
    0 讨论(0)
  • 2021-02-05 01:42

    MSDN File.Copy

    var fileName = "sourceFile.txt";
    var source = Path.Combine(Environment.CurrentDirectory, fileName);
    var destination = Path.Combine(destinationFolder, fileName);
    
    File.Copy(source, destination);
    
    0 讨论(0)
  • 2021-02-05 01:43
    string fileToCopy = "c:\\myFolder\\myFile.txt";
    string destinationDirectory = "c:\\myDestinationFolder\\";
    
    File.Copy(fileToCopy, destinationDirectory + Path.GetFileName(fileToCopy));
    
    0 讨论(0)
  • 2021-02-05 01:45
    File.Copy(@"someDirectory\someFile.txt", @"otherDirectory\someFile.txt");
    

    works fine.

    0 讨论(0)
  • 2021-02-05 01:57

    If the destination directory doesn't exist, File.Copy will throw. This version solves that

    public void Copy(
                string sourceFilePath,
                string destinationFilePath,
                string destinationFileName = null)
    {
           if (string.IsNullOrWhiteSpace(sourceFilePath))
                    throw new ArgumentException("sourceFilePath cannot be null or whitespace.", nameof(sourceFilePath));
           
           if (string.IsNullOrWhiteSpace(destinationFilePath))
                    throw new ArgumentException("destinationFilePath cannot be null or whitespace.", nameof(destinationFilePath));
           
           var targetDirectoryInfo = new DirectoryInfo(destinationFilePath);
    
           //this creates all the sub directories too
           if (!targetDirectoryInfo.Exists)
               targetDirectoryInfo.Create();
    
           var fileName = string.IsNullOrWhiteSpace(destinationFileName)
               ? Path.GetFileName(sourceFilePath)
               : destinationFileName;
    
           File.Copy(sourceFilePath, Path.Combine(destinationFilePath, fileName));
    }
    

    Tested on .NET Core 2.1

    0 讨论(0)
  • 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);
    
    0 讨论(0)
提交回复
热议问题