1

For an application I am writing, users have to select a folder containing some logfiles that the app works with. To make it more intuitive, I want them to be able to see the files in the folder, but still only be able to select the folder of course.

So my problem is, how can I have the Dialog open folders only, but still show the other files.

I have tried the solution from this thread,

How can I make CommonOpenFileDialog select folders only, but still show files?

However, it doesn't seem to work on Windows10, at least not for me and the other PCs I've tried it on.

This is what I currently have to open:

CommonOpenFileDialog dialog = new CommonOpenFileDialog();
dialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
dialog.IsFolderPicker = true;
if (dialog.ShowDialog() == CommonFileDialogResult.Ok)
{ }
ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
JohnH
  • 45
  • 8
  • what isn't working? – Steffen Winkler Aug 26 '19 at 08:19
  • 2
    If I am not wrong the `CommonOpenFileDialog` belong to the [Windows API Code Pack](https://stackoverflow.com/questions/24081665/windows-api-code-pack-where-is-it) package that is no more an official Microsoft release (and probably it never was). So it could stop to work in any future release. I suggest to not rely on these bits. – Steve Aug 26 '19 at 08:19
  • 1
    @SteffenWinkler Using the linked solution, the Dialog still only selects folders but doesn't display the other files. – JohnH Aug 26 '19 at 08:26
  • @Steve Ah, I see. By any chance would you know an alternative for C#? – JohnH Aug 26 '19 at 08:27
  • No I don't know of any alternative that will do what you are requesting. Probably an alternative approach is to use two different controls. On one you show the folders and their structure, in the other you show the files selected from the first control. Sorry, it is not a quick fix or something you would like to hear. – Steve Aug 26 '19 at 08:52
  • [Here](https://stackoverflow.com/questions/35159549/how-to-display-all-files-under-folders-in-treeview), there is something that could help you to start – Steve Aug 26 '19 at 08:54
  • Ah, I see. That's a shame, but thank you very much. I guess I'll see if the others think the trouble is worth it. – JohnH Aug 26 '19 at 09:00

1 Answers1

0

I found a comment by Daniel Ballinger to a similar question that may be what you are looking for:

How to use OpenFileDialog to select a folder?

The summary is that you can use OpenFileDialog in a hackish way to select both files and folders. Thus, it will allow your users to select folders showing the files in those folders.

Although it is not using CommonOpoenFileDialog the result is close to what you are asking.

It is a bit confusing though for users, since they can select a file accidentally!

Find here Select file or folder from the same dialog a wrapped solution made by Denis Stankovski.

  • 2
    By the way, I know that it would be better to add this as a comment, by I could not comment since I have no enough reputation, so I posted the answer, I think it may help you! – Andrés Fonts Apr 14 '20 at 15:24