-2

I want copy file from:

C:\Users\Machina\Documents\Visual Studio 2017\Projects\P\Patcher\bin\Debug\Patches\0.0.0.2\SomeDir\OtherDir\File.txt

to this Folder:

C:\Users\Machina\Documents\Visual Studio 2017\Projects\P\Patcher\bin\Debug\Builds

but i need to create subFolder in destination folder For this file:

\0.0.0.2\SomeDir\OtherDir\

so new path to file should be:

C:\Users\Machina\Documents\Visual Studio 2017\Projects\P\Patcher\bin\Debug\Builds\0.0.0.2\SomeDir\OtherDir\File.txt

I try

fileList[i].Replace(filePath, $"{path}Builds/")

but this return source file path :/ I don't have idea how do this.

2 Answers2

0

Are you possibly not assigning the result of the Replace function into a new variable and using that like below?

destPath = fileList[i].Replace(filePath, $"{path}Builds/");

/* now use destPath to create directory */
System.IO.Directory.CreateDirectory(destPath);

/* ... copy files to destPath ... */

The Replace function does not perform an in place replacement for fileList[i]. From MSDN (https://msdn.microsoft.com/en-us/library/fk49wtc1(v=vs.110).aspx):

This method does not modify the value of the current instance. Instead, it returns a new string in which all occurrences of oldValue are replaced by newValue.

If not, please add a code example so we can have a better idea of what you are needing.

  • I use this code: [link](https://pastebin.com/Mg2qKUit) I find solution for my problem. In destination folder i want create same like in source folder directory: `/0.0.0.2/` so i need only separate part of the source path path after `0.0.0.2`. Probably this will be the best solution. What do you think ? – Piotr Bibinin Mar 21 '18 at 14:10
  • Is your source path and destination path different by only the one directory name (i.e. **"c:\...\Patches\..."** becomes **"c:\...\Builds\..."**)? If so, I would just do a fileList[i].Replace(@"\Patches\", @"\Builds\") and use Directory.CreateDirectory to create all your folders like the answer here [link](https://stackoverflow.com/questions/2134392/how-to-create-multiple-directories-from-a-single-full-path-in-c/2134407). – user9525052 Mar 21 '18 at 18:27
0

Work for my: (this also methode resolve my other problem "use by another process exception").

using System;
using System.Collections.Generic;
using System.IO;

class COPYTEST
{
    string path = System.IO.Path.GetFullPath("./");
    private string splitString;


    public COPYTEST(string ver)
    {
        splitString = ver; // I need this for find split position in my case ver = "0.0.0.2";

    }

    public void Copy(List<string> dirList, List<string> fileList)
    {
        //Create dirs first
        for (int i = 0; i < dirList.Count; i++)
        {
            string dr = dirList[i].Substring(dirList[i].IndexOf($"{splitString}"));
            Directory.CreateDirectory(dirList[i].Replace(dirList[i], $"{path}Builds/{dr}"));
        }

        for (int i = 0; i < fileList.Count; i++)
        {
            string st = fileList[i].Substring(fileList[i].IndexOf($"{splitString}"));

            string sourceFilePath = fileList[i];
            string destinationFilePath = sourceFilePath.Replace(sourceFilePath, $"{path}Builds/{st}");

            int size = 2048 * 1024; //buffer size
            byte[][] buffer = new byte[2][];
            buffer[0] = new byte[size];
            buffer[1] = new byte[size];

            int current_read_buffer = 0; //pointer to current read buffer
            int last_bytes_read = 0; //number of bytes last read

            //Now create files
            try
            {
                using (FileStream reader = new FileStream(sourceFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, size * 2, FileOptions.SequentialScan))
                //using (FileStream fs = File.Open(<file-path>, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    FileStream writer = new FileStream(destinationFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite, size * 2, true);

                    current_read_buffer = 0;
                    last_bytes_read = reader.Read(buffer[current_read_buffer], 0, size); //synchronously read the first buffer
                    long l = reader.Length;
                    long a = 0;
                    while (a < l)
                    {
                        IAsyncResult aw = writer.BeginWrite(buffer[current_read_buffer], 0, last_bytes_read, new AsyncCallback(CopyFileCallback), 0);
                        current_read_buffer = current_read_buffer == 0 ? 1 : 0;
                        IAsyncResult ar = reader.BeginRead(buffer[current_read_buffer], 0, size, new AsyncCallback(CopyFileCallback), 0);
                        a += last_bytes_read;
                        last_bytes_read = reader.EndRead(ar);
                        writer.EndWrite(aw);
                    }

                    writer.Dispose();
                    reader.Dispose();
                }
            }
            catch (Exception ex)
            {
                //Log exception
            }
        }
    }
}