tags:

views:

1935

answers:

6

Hi,

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

+3  A: 

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.

Gishu
A: 

@Gishu is right, you can not do this. The only solution is the validation after selecting a file.

Biri
A: 

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.

Adam Rosenfield
Not correct. The CDN_INCLUDEITEM is sent only for pseudo-objects like namespace extensions, and not for ordinary files. This is very clear in the doc: "The dialog box always includes items that have both the SFGAO_FILESYSTEM and SFGAO_FILESYSANCESTOR attributes, regardless of the value returned by CDN_INCLUDEITEM. " See more discussion here: http://msdn.microsoft.com/en-us/magazine/cc300464.aspxdoc here: http://msdn.microsoft.com/en-us/library/ms646862(VS.85).aspxBottom line: CDN_INCLUDEITEM will not do what you suggest here.
Cheeso
A: 

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.

+2  A: 

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#.

Cheeso
A: 

You should be able to do it with the following filter: Data Files|*_1.xml

Pat