Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
439 views
in Technique[技术] by (71.8m points)

c# - File copy only x file extension

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.

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).

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'"

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!");
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
var files1 = Directory.GetFiles(GoPro1).Where(file => extensions.Contains(Path.GetExtension(file));   
foreach (file in files1)     
     File.Copy(file, Locatie + @"" + "GoPro1");

or:

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

And the Copy method:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...