Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse filter list for FileDialog

Tags:

c#

.net

If I have a filter string like this (filter for FileDialog):

"Image Files (*.bmp, *.jpg)|*.bmp;*.jpg|All Files (*.*)|*.*"

Is there a function in C# that gives me a list of all extensions in this filter?

This would be

"*.bmp", "*.jpg" and "*.*"
like image 356
MTR Avatar asked Jan 25 '26 07:01

MTR


2 Answers

You can do it with this regular expresion:

(?<Name>[^|]*)\|(?<Extension>[^|]*)\|?

Here is a sample code:

var regex = new Regex(@"(?<Name>[^|]*)\|(?<Extension>[^|]*)\|?");
var matches = regex.Matches(@"Image Files (*.bmp, *.jpg)|*.bmp;*.jpg|All Files (*.*)|*.*");
foreach (Match match in matches)
{
    Debug.Print("Name: '{0}' Extension:'{1}'", match.Groups["Name"].Value, match.Groups["Extension"].Value);
}
like image 199
Fischermaen Avatar answered Jan 27 '26 20:01

Fischermaen


I don't think so. However, it's easy enough to write one yourself.

public string[] getExtensions(string input) {
    if (string.isNullOrEmpty(input) || input.indexOf('|') == -1) {
        return null;
    }
    else {
        List<string> returnValue = new List<string>();
        string[] parts = input.Split('|')
        for(int x = 1; x < input.Length; x+=2) {
            foreach(string extension in parts[x].Split(',')) {
                returnValue.Add(extension);
            }
        }
        return returnValue.ToArray();
    }
}
like image 45
Andreas Eriksson Avatar answered Jan 27 '26 20:01

Andreas Eriksson