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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…