How can i get files from directory but of many extentions ?

  • Thread starter Thread starter Chocolade1972
  • Start date Start date
C

Chocolade1972

Guest
This is the code now after i changed it:


s = Environment.GetEnvironmentVariable("UserProfile") + "\\Pictures";
string[] extensions = { "*.bmp", "*.jpg", "*.png", "*.gif" };//add extensions you want to filter first
var photosfiles = extensions.SelectMany(x => Directory.GetFiles(s, x));
//string[] photosfiles = Directory.GetFiles(s, "*.*", SearchOption.AllDirectories);
for (int i = 0; i < photosfiles.ToArray().Length; i++)
{
FileInfo fi = new FileInfo((photosfiles.ToArray()));
DirectoryInfo d = new DirectoryInfo(tempphotos);
long dirSize = DirSize(d);
//if copying the file would take the directory over 50MB then dont do it
if ((dirSize + fi.Length) <= 24117248)
fi.CopyTo(tempphotos + "\\" + fi.Name,true);
else
break;
}


I want to get any photos files extentionsl ike: .jpg .bmp .png .gif

Before the changes the code was like this:


s = Environment.GetEnvironmentVariable("UserProfile") + "\\Pictures";

string[] photosfiles = Directory.GetFiles(s, "*.*", SearchOption.AllDirectories);
for (int i = 0; i < photosfiles.ToArray().Length; i++)
{
FileInfo fi = new FileInfo((photosfiles.ToArray()));
DirectoryInfo d = new DirectoryInfo(tempphotos);
long dirSize = DirSize(d);
//if copying the file would take the directory over 50MB then dont do it
if ((dirSize + fi.Length) <= 24117248)
fi.CopyTo(tempphotos + "\\" + fi.Name,true);
else
break;
}


This was working but it was getting all the files any files.


After i did the changes it never get to the copy line:


fi.CopyTo(tempphotos + "\\" + fi.Name,true);


Just jumping right away to the break;

What i wanted to do is :


1. Get only photos files extentions jpg png bmp gif

2. to check all the time the directory size and if its 24mb or more do a break


So the part that check the directory size is working the problem is that after i did the changes added the part that filter the photos extentions its not working anymore .


Also after the changes i had to add ToArray() here :


for (int i = 0; i < photosfiles.ToArray().Length; i++)
{
FileInfo fi = new FileInfo((photosfiles.ToArray()));


How can i solve the problem with the extentions filter ?

Continue reading...
 
Back
Top