将1个文件移动到多个文件夹 [英] Move 1 file to multiple folders
问题描述
我需要将1份pdf文件移至我所有的同事个人档案中。在发现许多代码与副本,但我总是复制到1个文件夹而不是多个。这些档案都在1个文件夹里。
我尝试过的事情:
i need to move 1 pdf file to all my coworkers personal dossier. in have found many codes with copy but i it always copy's to 1 folder an not multiple ones. the dossiers are all in 1 folder.
What I have tried:
string fileName = "test.txt";
string sourcePath = @"C:\Users\Public\TestFolder";
string targetPath = @"C:\Users\Public\TestFolder\SubDir";
Copy(sourcePath , targetPath)
// now i need to find a way to make the targetpath multi "targets'
推荐答案
其中一种方法是使用数组(作为一组目标子目录)。另一种使用List< string>的方法。
One of the way is to use array (as a set of target subdirs). Another way to use List<string>.
string fileName = "test.txt";
string sourcePath = @"C:\Users\Public\TestFolder";
string targetPath = @"C:\Users\Public\TestFolder\SubDir";
//define a range of "target" subdirs
string[] targets = new string[] {"sub1", "sub2", "sub3"};
//for each "target" in "targets"
foreach (string s in targets)
{
File.Copy(Path.Combine(sourcePath, fileName), Path.Combine(targetPath, s));
}
详情请见:
阵列教程(C#) [ ^ ]
List(T)Class(System.Collections.Generic) [ ^ ]
foreach,in(C#Reference) [ ^ ]
文件复制方法(System.IO) [ ^ ]
Path.Combine Method(System.IO) [ ^ ]
试试吧!祝你好运!
For further details, please see:
Arrays Tutorial (C#)[^]
List(T) Class (System.Collections.Generic)[^]
foreach, in (C# Reference)[^]
File.Copy Method (System.IO)[^]
Path.Combine Method (System.IO)[^]
Try! Good luck!
这篇关于将1个文件移动到多个文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!