Для тех, кто не хочет каждый раз вспоминать синтаксис, есть простая инкапсуляция:
public class FileDialogFilter : List<string>
{
public string Explanation { get; }
public FileDialogFilter(string explanation, params string[] extensions)
{
Explanation = explanation;
AddRange(extensions);
}
public string GetFileDialogRepresentation()
{
if (!this.Any())
{
throw new ArgumentException("No file extension is defined.");
}
StringBuilder builder = new StringBuilder();
builder.Append(Explanation);
builder.Append(" (");
builder.Append(String.Join(", ", this));
builder.Append(")");
builder.Append("|");
builder.Append(String.Join(";", this));
return builder.ToString();
}
}
public class FileDialogFilterCollection : List<FileDialogFilter>
{
public string GetFileDialogRepresentation()
{
return String.Join("|", this.Select(filter => filter.GetFileDialogRepresentation()));
}
}
Использование:
FileDialogFilter filterImage = new FileDialogFilter("Image Files", "*.jpeg", "*.bmp");
FileDialogFilter filterOffice = new FileDialogFilter("Office Files", "*.doc", "*.xls", "*.ppt");
FileDialogFilterCollection filters = new FileDialogFilterCollection
{
filterImage,
filterOffice
};
OpenFileDialog fileDialog = new OpenFileDialog
{
Filter = filters.GetFileDialogRepresentation()
};
fileDialog.ShowDialog();