MaxTheCoder
MaxTheCoder

Reputation: 135

C# OpenFileDialog multiple filename filters including exclude

I have a requirement to allow users to open a specific file for processing. The open file dialog is currently

        OpenFileDialog ofg = new OpenFileDialog
        {
            FileName = "BaseFileName*",
            Filter = "CSV File (*.CSV)|*.csv",
            Multiselect = false,
            InitialDirectory = @"N:\Downloads"
        }; 

However the process adds a suffix of _Processed along with timestamp data to the filename and I want to exclude these renamed files the next time the OpenFileDialog is used to prevent the user trying to reprocess the same file.

I have to leave the original files where they are for internal audit reasons.

So I need an additional filename filter of not equal to "_Processed".

Is there any way to do this with OpenFileDialog or does anyone know of a custom c#/.net component that can do this?

Upvotes: 0

Views: 511

Answers (2)

Tha_slughy
Tha_slughy

Reputation: 1

The easy way is just saving the processed data with another extension e.g. "BaseFileName_Processed_20105640640.cvs1", that way you keep the data and your file dialog will not show this file.

Another way could be to call the OpenFileDialog() in an if statement (and compare the return to DialogResult.OK), then split the file name for {'_','.'}, then run a loop to count the occurrences of the word Processed( >0), and possibly as a safety check determine whether a timestamp is present in one of the split strings. Finally, reload the FileOpenDialog in the same folder when the wrong file was selected.

Upvotes: 0

dialer
dialer

Reputation: 4835

You are asking to omit specific items from the file dialog view.

According to MSDN, this is no longer possible as of Windows 7, but was possible previously.

The C# file dialogs (both WPF and WinForms) use the IFileDialog API.

Here is the function that could have made this work, but is no longer supported:

https://learn.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-ifiledialog-setfilter

As it is, you are stuck with checking the file for correctness after the user has already selected it and confirmed it with OK.

You can help the situation a little bit: If you enjoy pain, then you can copy the whole IFileDialog COM interop code from the .NET source code, and implement IFileDialogEvents. This way, when the user clicks "OK", you can deny the selection and display an error before the dialog closes, leaving the dialog open so the user can select a different file.

If you are sane and you don't want to do that, then you'll have to open the dialog again after the verification fails.

Upvotes: 2

Related Questions