0

I am new to C# and have a project where I have a List String and have to filter some images out with certain extensions . This is my code

// App Config
<add key="FilterImages" value=".jpg ,.gif"/>

List<string> _FilterList = new List<string>();
string[] FilterList = ConfigurationManager.AppSettings["FilterImages"].ToString().Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string s in FilterList)
{
    _FilterList.Add(s.Trim().ToLower());
}

var files = newFiles.ListDirectory(".");
foreach (var f in files)
{
    // Here new files come and I can get the file names using
    // f.name ... How can I check for correct extension here   
}

Inside the ForEach loop I would like to use _FilterList and check the file extension of the new files coming in . I can get the file names using f.Name any suggestions would be great

1

3 Answers 3

2

First, let's obtain extensions to be filtered out; HashSet<string> seems to be a better collection fo this

HashSet<string> extensions = new HashSet<string>(ConfigurationManager
    .AppSettings["FilterImages"].ToString()
    .Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
    .Select(item => item.Trim()), 
  StringComparer.OrdinalIgnoreCase); // ".GIF" should be treated as ".gif"

Then we can start checking files:

var files = newFiles.ListDirectory(".");

foreach (var f in files) {
  // if "f" is FileInfo we can use f.FullName
  // if "f" is string then put just f, not f.FullName 
  if (extensions.Contains(Path.GetExtension(f.FullName))) {
    // file has a correct extension 
  } 
}
Sign up to request clarification or add additional context in comments.

Comments

2

Try some thing like this , Just look for file extension in your FilterList. This may not compile but its an idea how to look for valid file extension.

foreach (var f in files)
{
    if(_FilterList.Contains(Path.GetExtension(f.FileName))
    {
       //Do your stuff
    }
}

Or you can use Linq

 var allowedFiles =  files.Where(p => _FilterList.Any(l => p.Contains(l)).ToList();

1 Comment

Would have been my solution too. However there are some caveeats: Case Sensitivty is a thing. And as foreach uses a Enumerator under the hood, you can not modify the collection. So 'your stuff' can not be a Remove operation on files.
1

First, you can factorize your code using LinQ to :

var _FilterList = ConfigurationManager.AppSettings["FilterImages"].ToString()
     .Split(',')
     .Select(p => p.Trim().ToLower())
     .ToList();

var files = newFiles.ListDirectory(".");

And getting Filtered files path with :

var acceptedFiles = files.Where(p => _FilterList.Any(l => p.Contains(l)).ToList();
var unAcceptedFiles = files.Where(p => !_FilterList.Any(l => p.Contains(l)).ToList();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.