[英]C# Copy User Defined Number of Files
I am attempting to copy a set number of files from one directory to another. 我试图将一组文件从一个目录复制到另一个目录。 My code in its current state copies all files within a directory.
我的代码处于当前状态,将复制目录中的所有文件。 I believe I would need a list, or an array, but am somewhat new to C#, so wanted to bring my question here.
我相信我需要一个列表或一个数组,但是对于C#来说有些陌生,所以想在这里提出我的问题。 An example would be to copy 20 files from the directory specified in the code.
一个示例是从代码中指定的目录中复制20个文件。 Any help would be appreciated.
任何帮助,将不胜感激。 Thanks!
谢谢!
static void Main(string[] args)
{
}
private void CopyFiles(int numberOfFiles)
{
List<string> files = System.IO.Directory.GetFiles(@"C:\Users\acars\Desktop\A", "*").ToList();
IEnumerable<string> filesToCopy = files.Where(file => file.Contains("Test_Test")).Take(20);
foreach (string file in filesToCopy)
{
// here we set the destination string with the file name
string destfile = @"C:\Users\acars\Desktop\B\" + System.IO.Path.GetFileName(file);
// now we copy the file to that destination
System.IO.File.Copy(file, destfile, true);
};
With a few modifications your code is able to copy a given number of files. 经过一些修改,您的代码就能复制给定数量的文件。 The following example takes the first x files from your directory:
以下示例从您的目录中获取前x个文件:
private void CopyFiles(int numberOfFiles)
{
List<string> files = System.IO.Directory.GetFiles(@"C:\Users\rando\Desktop\A", "*").ToList();
IEnumerable<string> filesToCopy = files.Where(file => file.Contains("Test_Test")).Take(numberOfFiles);
foreach (string file in filesToCopy)
{
// here we set the destination string with the file name
string destfile = @"C:\Users\rando\Desktop\B\" + System.IO.Path.GetFileName(file);
// now we copy the file to that destination
System.IO.File.Copy(file, destfile, true);
}
}
If you want the first x files according to a special order, you have to order the "files" list first. 如果要按特殊顺序订购前x个文件,则必须先订购“文件”列表。
Since you need to order by information about the file beyond the name, you'll need to use FileInfo
. 由于您需要按名称以外的文件信息进行排序,因此需要使用
FileInfo
。 Here's what I believe to be a straightforward implementation that should get you started. 我认为这是一个应该让您入门的简单实现。
static void Main(string[] args)
{
var desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
var path = Path.Combine(desktop, "A");
var outPath = Path.Combine(desktop, "B");
Copy(20, "file", path, outPath, x => x.LastWriteTimeUtc);
}
static void Copy<T>(int count, string filter, string inputPath, string outputPath, Func<FileInfo, T> order)
{
new DirectoryInfo(inputPath).GetFiles()
.OrderBy(order)
.Where(file => file.Name.Contains(filter))
.Take(count)
.ToList()
.ForEach(file => File.Copy(file.FullName, Path.Combine(outputPath, file.Name), true));
}
To use this, you would pass in the number to copy, the paths, a filter, and an ordering action. 要使用此功能,您需要输入要复制的数字,路径,过滤器和排序动作。
new DirectoryInfo(inputPath).GetFiles()
- Get a FileInfo[]
of every file in the specified path new DirectoryInfo(inputPath).GetFiles()
-获取指定路径中每个文件的FileInfo[]
.OrderBy(order)
- order them .OrderBy(order)
-订购他们 .Where(file => file.Name.Contains(filter))
- filter by the passed in filter .Where(file => file.Name.Contains(filter))
-通过传入的过滤器进行过滤 .Take(count)
- take the count .Take(count)
-计数 .ToList()
- convert to List to use ForEach .ToList()
-转换为List以使用ForEach .ForEach(file => File.Copy(file.FullName, Path.Combine(outputPath, file.Name), true))
- Make the File.Copy() call for each .ForEach(file => File.Copy(file.FullName, Path.Combine(outputPath, file.Name), true))
-对每个文件调用File.Copy()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.