I have a Combobox for choosing file extensions like "Images, (*.png, *.jpg)". I want to get the data from a Dictionary key/value pair. I have added string for the first part, for instance "Images" and for extensions I added a list since there can be more than one. I use this data in showing SaveFileDialog or OpenFileDialog. How can I use these extensions as a filter for SaveFileDialog? Could you please provide help?
ExtensionCollection = new Dictionary<string, IList<String>>();
ExtensionTypeCollecction = new List<String>();
Extensions = new List<IList<String>>();
perExtension = new List<String>();
perExtension.Add("*.png");
perExtension.Add("*.jpg");
Extensions.Add(perExtension);
ExtensionTypeCollecction.Add("Images");
ExtensionCollection.Add("Images", perExtension);
Thanks in advance
I think this is what you're after:
var d = new Dictionary<string, IList<string>>();
d.Add("Images", new List<string>{ "*.png", "*.jpg" });
var key = "Images";
var extensions = d["Images"];
var filter = key + "|" + string.Join(";", extensions.ToArray());
giving
filter = "Images|*.png;*.jpg"
or you can do this
var extString = string.Join(";", extensions.ToArray());
var filter = key +" (" + extString + ")|" + extString;
which results in
filter = "Images (*.png;*.jpg)|*.png;*.jpg"
and you can of course add the usual "|All files (*.*)|(*.*)"
filter option if required.
Does this help?
foreach (string ext in Dictionary["images"])
{
Debug.WriteLine(ext);
}