文件仅复制x文件扩展名

时间:2014-05-26 10:14:36

标签: c# file

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

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

其他一些人告诉我,我需要补充: var extensions = new [] {" .MP4"," .LRV"," .THM" }; var files1 = Directory.GetFiles(GoPro1).Where(file => extensions.Contains(new FileInfo(file).Extension)); 对于第一个方法..但是这没有得到下一个错误:"无法从' String []'转换为' String'"

我想我需要在方法中添加一个循环: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!");
    }

2 个答案:

答案 0 :(得分:-1)

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

答案 1 :(得分:-1)

解决了!

在最后一种方法中:Copyall: foreach(FileInfo fi in source.GetFiles(&#34; * .MP4&#34;))                 {

                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();
            }