仅文件复制x文件扩展名 [英] File copy only x file extension

查看:76
本文介绍了仅文件复制x文件扩展名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可以复制文件的有效方法,但是我想为其添加一个额外的功能. 我只想复制以下文件扩展名:*.mp4*.LRV*.THM.

I have a working method which copies my files, but I want to add a extra function to it. I want to copy only these file extensions:*.mp4, *.LRV and *.THM.

您可以在下面看到有2个方法和一个if ..因此有3个方法(未复制第一个方法的所有内容,因为它们不相关).

You can see below that there are 2 methodes and a if.. so there are 3 methodes(did not copy everything of the first methode because it issn't relevant).

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

Some other guy told me that i need to add: var extensions = new[] { ".MP4", ".LRV", ".THM" }; var files1 = Directory.GetFiles(GoPro1).Where(file => extensions.Contains(new FileInfo(file).Extension)); To the first methode.. but this issn't right i get the next error:"Cannot convert from 'String[]'to'String'"

我认为我需要在方法中添加一个循环:copyall.但是我不知道我必须做什么样的循环.有人可以帮我解决这个问题吗?

I think i need to add a loop in the methode: copyall. But i don't know what kind of loop i must make. can someone please help me out with this problem?

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!");
    }

推荐答案

解决了!

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

In the last methode: Copyall: 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();
            }

这篇关于仅文件复制x文件扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆