使用创建新的子目录将文件复制到另一个位置,但在目标文件夹中的相同子目录中



我想从以下位置复制文件:

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

到此文件夹:

C:\Users\Machina\Documents\Visual Studio 2017\项目\P\补丁程序\bin\调试\构建

但我需要在目标文件夹中创建子文件夹 对于此文件:

\

0.0.0.2\SomeDir\OtherDir\

所以文件的新路径应该是:

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

我尝试

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

但是这个返回源文件路径:/我不知道这是怎么做到的。

您是否可能没有将 Replace 函数的结果分配给新变量并像下面这样使用它?

destPath = fileList[i].Replace(filePath, $"{path}Builds/");
/* now use destPath to create directory */
System.IO.Directory.CreateDirectory(destPath);
/* ... copy files to destPath ... */
替换

函数不执行文件列表[i]的就地替换。 来自 MSDN (https://msdn.microsoft.com/en-us/library/fk49wtc1(v=vs.110(.aspx(:

此方法不会修改当前实例的值。相反,它返回一个新字符串,其中所有出现的 oldValue 都替换为 newValue。

如果没有,请添加一个代码示例,以便我们更好地了解您的需求。

为我的工作:(这也解决了我的另一个问题"由另一个进程异常使用"(。

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

相关内容

最新更新