简体   繁体   中英

Regular expression to filter files in OpenFileDialog

I would like to know how to filter files in a open file dialog (in winforms) based on a regular expression. Files have all same extensions (.xml). Big files are split up into several files with the same name only to be separated with _1 ... We only want to show the files without _1 (first data file)

the open file dialog has a property filter but i dont know how to specify this in our filename format, hence the regular expression.

Thankx, Niki

I don't think you can do it with the OpenFileDialog's Filter property, which just filters list of files based on extension.

I think you'll have to let the user choose an xml file, validate and then pop up the dialog again if its a _1 file. You can subscribe to the FileOK event and slot in this validation in there. You can use regular expressions to validate the filename here. That's the best that can be done.. I guess.

The OpenFileDialogEx described in this CodeProject article is an extension of the standard OpenFileDialog. The primary intention of that extension is to modify the display of the dialog, but there are some additional bells and whistles. For example, OFDEx adds a few events, for File changed, Folder change, etc.

Someone pointed out that the CDN_INCLUDEITEM notification seems like it would satisfy the desire to filter the list of files shown in the dialog,. It seems like it would, but it does not. The CDN_INCLUDEITEM does not do what you might think or want.

According to this MSDN Mag article ,

If you create your dialog with OFN_ENABLEINCLUDENOTIFY, Windows sends your hook procedure a CDN_INCLUDEITEM notification for every item it adds to the open list. If you return FALSE, Windows excludes the item. The problem is, Windows doesn't notify you for ordinary files, only pseudo-objects like namespace extensions. When you read the documentation through a magnifying glass, the print is perfectly clear: "The dialog box always includes items that have both the SFGAO_FILESYSTEM and SFGAO_FILESYSANCESTOR attributes, regardless of the value returned by CDN_INCLUDEITEM." Apparently the Redmondtonians added CDN_INCLUDEITEM for their own purposes, which didn't include filtering ordinary file names.

In other words, in response to CDN_INCLUDEITEM, you cannot return FALSE for regular files, to exclude them from the dialog. In contrast to the doc which says, the response from the CDN_INCLUDEITEM is ignored for regular files, in my experience, the CDN_INCLUDEITEM is not even sent for regular files, at least not on my Vista machine.

So is it possible to exclude files dynamically? Well, yes, in C++; In response to the CDN_FOLDERCHANGED message, you can get and set the contents of the CListCtrl that contains the files. I haven't figured out how to set this list in C#.

The OpenFileDialog does not support this. An alternative is to use a 3rd party control like FileView which lets you filter items using any criteria you wish such as regular expressions.

您应该能够使用以下过滤器:数据文件| * _1.xml

I'm not sure how to do it in C# with WinForms, but in C++, what you would do is install a custom hook procedure and listen for the CDN_INCLUDEITEM notification. Then, you check each filename against your regex. See http://msdn.microsoft.com/en-us/library/ms646960(VS.85).aspx#_win32_Filters .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM