Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
384 views
in Technique[技术] by (71.8m points)

.net - 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?

question from:https://stackoverflow.com/questions/65940292/c-sharp-openfiledialog-multiple-filename-filters-including-exclude

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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://docs.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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...