I'm trying to read a list of documents in a directory. I understand how the filter modifiers work at a high level and have used them before. However, this time I need to filter something that doesn't have a standardized extension (i.e. "*.txt" or *.pdf").
I'm using the example as found on.... https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.filedialog.filter?view=net-5.0
using System;
using System.Windows.Forms;
namespace COMS2412_Readin
{
public partial class Form1 : Form
{
string Serial_num = null;
string INI_file_dir_temp = null;
string CONFIG_data = null;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//Allow user to enter serial number and load it for useage later.
Serial_num = Serial_number_text.Text;
try
{
using (OpenFileDialog COMS_file_dialog = new OpenFileDialog())
{
COMS_file_dialog.InitialDirectory = "c:\\";
//Have dialog box filter out all files without the .INI extention.
COMS_file_dialog.Filter = "txt files (*.INI)|*.INI|All files (*.*)|*.*"; //Failed
// COMS_file_dialog.Filter = "*.INI"; //Failed
// COMS_file_dialog.Filter = "(*.INI)"; //Failed
// COMS_file_dialog.Filter = "(INI files | *.INI)"; //Failed
COMS_file_dialog.FilterIndex = 2;
COMS_file_dialog.RestoreDirectory = true;
if (COMS_file_dialog.ShowDialog() == DialogResult.OK)
{
//Check to make sure it was the correct file type.
INI_file_dir_temp = COMS_file_dialog.FileName;
}
}
}
catch
{
//Sorry there was an error with opening the file. Please ensure the file is not currently open.
}
}
}
}
The project I'm working on imports config files from an external piece of equipment. The problem is there are literally thousands of files, each with unusual file extensions. For example *.INI
is one of the extensions I want to filter. I've tried just entering `*.INI as the argument but it didn't work. What am I missing? Is there a way to alter...
openFileDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
to filter specific file extensions?
*.INI
? Why don't you post the real code only? Answering your comment, you need to wrap your code in ticks(or use the code-button in the editor) to avoid that it will be interpreted in some way. I have edited your question accordingly. – Tim Schmelter Mar 17 at 21:49