5

I have the following code in my C# program:

        OpenFileDialog fDialog = new OpenFileDialog();
        fDialog.Title = "Open a file";
        fDialog.Filter =
           "NCF files (*.ncf)|*.ncf|All files (*.*)|*.*|No Extensions (*.)|*.";



I want to be able to have the user pick from the following:

*.NCF (files with .NCF extension only)
**.* (all files)

and files that have no extensions such as:

filewithnoextension



I know ***.* will do this, but it also displays the .NCF, .TXT, and all other files in the same directory.

I just want to be able to display filenames that have no extensions.



Filtering with *. does not do the trick. It works fine when doing it with a DOS window (dir *.) , but C# seems to ignore the *. filter.





Is there a way I can do this with C#?

Thanks.

fraXis
  • 3,141
  • 8
  • 44
  • 53

4 Answers4

3

I know this works:

fDialog.Filter = "No extension Files|" + null;

I haven't tested with multiple selections..

Altough this is an old post, I though it would benefit someone looking for a way to only display files with no extensions..

Matimont
  • 739
  • 3
  • 14
  • 33
1

A readme file normally has an extension. I suppose you did, but did you check this folder option to see the extensions of known file types? Has it changed anything?

EDIT #1

Frankly, I doubt you'd be able to make the OpenFileDialog display files with no extension, as the Filter property is based on the extension.

Perhaps you could inherit base your own implemented OpenFileDialog using the System.IO namespace objects such as DirectoryInfo, for instance, which will allow you to get the browsed folder files with the Getfiles() method, then filter yourself through LINQ to display the files with no extension only with the FileInfo.Extension property.

EDIT #2

Since the OpenFileDialog is sealed, you may use it as a nested type and implement your own methods using this nested type.

I hope this helps you!

Will Marcouiller
  • 23,773
  • 22
  • 96
  • 162
  • I am just using readme as an example. I am going to change it in my post because it is not a good example. – fraXis May 18 '10 at 01:07
0

I thought using *. would work, but it doesn't, so it seems that's a limitation of the OpenFileDialog control.

You could create your own dialog but the OpenFileDialog is not inheritable so this would end up being a lot of work for just a small feature.

Is the file with no extension created by your own application? If that's the case, you could give it a custom extension for your filtering. If it's not, then I'm afraid I can't think of anything else to help you :(

Good luck!

vitorbal
  • 2,871
  • 24
  • 24
  • No. Another software program is creating the files that have no extensions. My client just wants to be able to see the files that have no extensions so he can easily open them in a directory with other files that have extensions. – fraXis May 18 '10 at 01:39
  • 1
    Even though the OpenFileDialog is sealed (not inheritable), you may use it as a nested type. For instance, using a property that will get the NativeDialog. Then, you write your method always using the NativeDialog property and you're done. – Will Marcouiller May 18 '10 at 10:47
0

If the other software program creates these files in the same location, why not have your code add an extension (something innocuous like ".XXX") to every extension-less file in that folder, and then show the dialog?

Edit: Or, see this MSDN article:

http://msdn.microsoft.com/en-us/library/ms646960(VS.85).aspx

From the Filters section:

The CDN_INCLUDEITEM notification message provides another way to filter the names that the dialog box displays. To use this message, provide an OFNHookProc hook procedure and specify the OFN_ENABLEINCLUDENOTIFY flag in the OPENFILENAME structure when you create the dialog box. Each time the user opens a folder, the dialog box sends a CDN_INCLUDEITEM notification to your hook procedure for each item in the newly opened folder. The return value of the hook procedure indicates whether the dialog box should display the item in the folder's item list.

Down at the bottom in the Explorer-Style Hook Procedures section, the article explains how to do this. Basically, you pass an event handler to the OpenFile dialog, and each time the user navigates to a new folder, the dialog iterates through all the files in the folder and calls your event handler for each one. Inside the event handler, you would put your code to determine if the file has an extension or not, and return true or false, accordingly.

MusiGenesis
  • 74,184
  • 40
  • 190
  • 334
  • Hi, i'm looking for answer to this problem, someone is implemented it as your idea? it is working?? how to do it?? – David Michaeli Jul 05 '12 at 17:32
  • @DavidMichaeli: I didn't implement this, so I don't know whether it works or not. The link I included in my answer shows how to do this. – MusiGenesis Jul 05 '12 at 20:37
  • Unfortuanally, this solution does not work. CDN_INCLUDEITEM allows filter only some synthetic extensions. File system objects will be included whatever you will return from CDN_INCLUDEITEM handler. – Serge Z Aug 14 '13 at 06:16