Its pretty simple, just add your filter like this
openFileDialog.Filter = "Office Files(Document or Excel)|*.doc;*.docx;*.xlsx;*.xls|Word Document(*.doc *.docx)|*.doc;*.docx";
var result = openFileDialog.ShowDialog();
if (result == DialogResult.OK)
{
var selectedFile = openFileDialog.FileName;
var filterIndex = openFileDialog.FilterIndex;
if(filterIndex == 1)
{
/* Code to perform if first filter (Office files in this case) is selected */
}
else if (filterIndex == 2)
{
/* Code to perform if second filter (Word Document in this case) is selected */
}
Here you can see that * .doc and * .docx are repeated. Therefore, based on the selected value, you can decide which encoding (in your case) to apply.
source
share