文件复制只能x文件扩展名

本文关键字:文件 扩展名 复制 | 更新日期: 2023-09-27 18:05:14

我有一个工作方法,复制我的文件,但我想添加一个额外的功能。我只想复制这些文件扩展名:*.mp4, *.LRV*.THM

你可以看到下面有两个方法和一个if…所以有3个方法(没有复制第一个方法的所有内容,因为它不相关)。

另一个人告诉我,我需要添加:var extensions = new[] {". mp4 ", ". mp4 "。液压制动"、"。三卤甲烷"};var files1 = Directory.GetFiles(GoPro1)。其中(file => extensions.)包含(新FileInfo(文件).Extension));对于第一种方法…但这是不对的,我得到下一个错误:"不能从'字符串[]'转换为'字符串'"

我想我需要在方法:copyall中添加一个循环。但我不知道我该做什么样的循环。有人能帮我解决这个问题吗?

if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  {
  Locatie = dlg.SelectedPath;
  var extensions = new[] { ".MP4", ".LRV", ".THM" };
  var files1 = Directory.GetFiles(GoPro1).Where(file => extensions.Contains(new FileInfo(file).Extension));   
  Copy1(files1, Locatie + @"'" + "GoPro1");
  }
public void Copy1(string sourceDirectory, string targetDirectory){
    DirectoryInfo diSource = new DirectoryInfo(sourceDirectory);
    DirectoryInfo diTarget = new DirectoryInfo(targetDirectory);
    //Gets size of all files present in source folder.
    GetSize(diSource, diTarget);
    maxbytes = maxbytes / 1024;
    progressBar1.Maximum = maxbytes;
    CopyAll(diSource, diTarget);
}
    public void CopyAll(DirectoryInfo source, DirectoryInfo target)
    {
        if (Directory.Exists(target.FullName) == false)
        {
            Directory.CreateDirectory(target.FullName);
        }
        foreach (FileInfo fi in source.GetFiles())
        {
            fi.CopyTo(Path.Combine(target.ToString(), fi.Name), true);
            total += (int)fi.Length;
            copied += (int)fi.Length;
            copied /= 1024;
            progressBar1.Step = copied;
            progressBar1.PerformStep();
            label1.Text = (total / 1048576).ToString() + "MB van de " + (maxbytes / 1024).ToString() + "MB gekopieërd";
            label1.Refresh();
        }
        foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
        {
            DirectoryInfo nextTargetSubDir = target.CreateSubdirectory(diSourceSubDir.Name);
            CopyAll(diSourceSubDir, nextTargetSubDir);
        }
        MessageBox.Show("Het kopieren is klaar!");
    }

文件复制只能x文件扩展名

var files1 = Directory.GetFiles(GoPro1).Where(file => extensions.Contains(Path.GetExtension(file));   
foreach (file in files1)     
     File.Copy(file, Locatie + @"'" + "GoPro1");

或:

var files1 = New DirectoryInfo(GoPro1).EnumerateFiles.Where(file => extensions.Contains(Path.GetExtension(file));   
Copy1(files, Locatie + @"'" + "GoPro1'")     

Copy法:

public void Copy1(IEnumerble<FileInfo> files, string targetDirectory)
{
    maxbytes = files.Sum(x => x.Lenght) / 1024;
    progressBar1.Maximum = maxbytes;        
    foreach(file in files)
    {
       file.Copy(targetDirectory + file.Name)
       ... report progress to ProgressBar
    }
}

解决了!

在最后一个方法中:foreach (FileInfo fi in source.GetFiles("*.MP4"))){

                fi.CopyTo(Path.Combine(target.ToString(), fi.Name), true);
                total += (int)fi.Length;
                copied += (int)fi.Length;
                copied /= 1024;
                progressBar1.Step = copied;
                progressBar1.PerformStep();
                label1.Text = (total / 1048576).ToString() + "MB van de " + (maxbytes / 1024).ToString() + "MB gekopieërd";
                label1.Refresh();
            }