我想要复制文件:
C:\ Users \ Machina \ Documents \ Visual Studio 2017 \项目\ P \修补程式\ BIN \调试\补丁\ 0.0.0.2 \ SomeDir \ OtherDir \ FILE.TXT
到此文件夹:
C:\ Users \ Machina \ Documents \ Visual Studio 2017 \项目\ P \修补程式\ BIN \调试\构建
但我需要在目标文件夹中创建subFolder对于此文件:
\ 0.0.0.2 \ SomeDir \ OtherDir \
所以文件的新路径应该是:
C:\ Users \ Machina \ Documents \ Visual Studio 2017 \项目\ P \修补程式\ BIN \调试\构建\ 0.0.0.2 \ SomeDir \ OtherDir \ FILE.TXT
我试试
$e = $ErrorActionPreference
$ErrorActionPreference="stop"
$E_Subnet_1 = '10.0.1'
$E_Subnet_2 = '10.0.2'
$O_Subnet_1 = '10.11.1'
$O_Subnet_2 = '10.11.2'
$D_Subnet_1 = '10.12.1'
$D_Subnet_2 = '10.12.2'
$Ethernet0 = 'Ethernet0'
$All_Subnets = @("$E_Subnet_1", "$E_Subnet_2", "$O_Subnet_1",
"$O_Subnet_2", "$D_Subnet_1", "$D_Subnet_2")
$result = (Get-NetAdapter |
? status -eq 'up' |
Get-NetIPAddress -ErrorAction 0 |
? PrefixOrigin -eq 'Manual' |
? IPAddress -match $All_Subnets |
foreach { $Ethernet0 -eq $_.InterfaceAlias})
Write-Host "interface_alias=$result"
但是这个返回源文件路径:/我不知道如何做到这一点。
答案 0 :(得分:0)
你是否可能没有将Replace函数的结果分配给一个新变量并使用如下所示?
destPath = fileList[i].Replace(filePath, $"{path}Builds/");
/* now use destPath to create directory */
System.IO.Directory.CreateDirectory(destPath);
/* ... copy files to destPath ... */
Replace函数不会对fileList [i]执行替换替换。来自MSDN(https://msdn.microsoft.com/en-us/library/fk49wtc1(v=vs.110).aspx):
此方法不会修改当前实例的值。相反,它返回一个新字符串,其中所有出现的oldValue都被newValue替换。
如果没有,请添加一个代码示例,以便我们更好地了解您的需求。
答案 1 :(得分:0)
为我工作:(这也解决了我的另一个问题“由另一个进程异常使用”)。
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
}
}
}
}