4

So I recently tried to the FolderBrowserDialog but much to my disappointment it was not like the following screenshot:

http://i.imgur.com/s2LHqxA.png

But instead it was formatted and as I think, hard to navigate with like this: http://i.imgur.com/rfSnt8C.png

How would I go about getting the other version where it's a dialog box asking for what folder to save to like the select file type natively, instead of what I think is this hard to navigate menu.

Codingale
  • 220
  • 6
  • 17
  • are you using winforms or wpf? – Saverio Terracciano Apr 28 '15 at 11:03
  • AFAIK `FolderBrowserDialog` used to be like second screen shot only. If you want something different you have to roll your own. – Sriram Sakthivel Apr 28 '15 at 11:04
  • To be honest, I have no clue what I'm using. I'm very new to this and this is probably the 3rd or 4th time I've programmed in C#.... infact this is the first time I've had to work with invoke. – Codingale Apr 28 '15 at 11:08
  • @PanagiotisKanavos I don't quite know how to change it to that, any suggestions? – Codingale Apr 28 '15 at 11:09
  • 1
    Please show some code, since you shouldn't be working with invoke to get the OpenXXDialog inside C# – Bernd Linde Apr 28 '15 at 11:15
  • Oh no, I meant I had to use invoke for the first to call a function I defined on another thread for logging output to a text box. – Codingale Apr 28 '15 at 11:18
  • 1
    @BerndLinde the OP isn't working with Invoke, the desired folder is actually the Vista-style File Dialog which isn't available in .NET. In fact, using native calls *is* one of the solutions to this. – Panagiotis Kanavos Apr 28 '15 at 11:18
  • There are many similar questions, eg [this](http://stackoverflow.com/questions/9227917/how-to-use-open-file-dialog-to-select-a-folder). The easiest option, using the Windows API Code pack is tricky because MS pulled the code a while ago. Many people uploaded the code to GitHub though and you can find unofficial NuGet packages, eg [this one](https://www.nuget.org/packages/WindowsAPICodePack-Shell/) – Panagiotis Kanavos Apr 28 '15 at 11:21
  • @PanagiotisKanavos Yes, I just installed it after browsing related questions how ever I'm clueless how to use it... **Edit** I think I found out thanks to your link. **Edit2:** Odd if I add it to my use list it's not a valid class still. – Codingale Apr 28 '15 at 11:24
  • I'm still looking for a native way instead of this, it works but it's still not quite desired.. anything else? I could accept the WindowsAPICodePack I suppose. – Codingale Apr 28 '15 at 12:03

2 Answers2

9

The CommonOpenFileDialog class from the NuGet Package "Microsoft.WindowsAPICodePack-Shell" will answer your request.

Set IsFolderPicker property to true and that's it.

using Microsoft.WindowsAPICodePack.Dialogs;     

private bool SelectFolder(out string fileName)
{
    CommonOpenFileDialog dialog = new CommonOpenFileDialog();
    dialog.IsFolderPicker = true;
    if (dialog.ShowDialog() == CommonFileDialogResult.Ok)
    {
        fileName = dialog.FileName;
        return true;
    }
    else
    {
        fileName = "";
        return false;
    }
}
Foole
  • 4,754
  • 1
  • 26
  • 23
Tal Halamish
  • 106
  • 1
  • 4
  • `like the select file type natively` from the original question but thanks for an answer anyways that's surely to help others. This requires a NuGet package and including a things to the project rather than a native method. – Codingale Mar 10 '16 at 17:08
1

thats because you are using FolderBrowserDialog instead of OpenFileDialog

you can check the below

 private void btnBrowse_Click(object sender, EventArgs e)
        {
            OpenFileDialog fileDialog = new OpenFileDialog();
            fileDialog.Title = "Browse File";
            fileDialog.Filter = "All files (*.*)|*.*|All files (*.*)|*.*";
            fileDialog.FilterIndex = 2;
            fileDialog.InitialDirectory = "c:\\";
            fileDialog.RestoreDirectory = true;

            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                txtFileName.Text = fileDialog.FileName;
            }
        }
nayef harb
  • 753
  • 1
  • 10
  • 19
  • 1
    This seems to select a file, and by the rest of the copy pasted code it seems to be reused from a `.txt` file but changed for All files `*.*` – Codingale Apr 28 '15 at 12:04
  • you can substring the folder name from it. the interface you need isn't provided for folder selection Check this link http://stackoverflow.com/questions/9227917/how-to-use-open-file-dialog-to-select-a-folder. – nayef harb Apr 28 '15 at 12:07
  • `txtFileName` shows this is probably from code to select a `txt` filetype. I'm still doing research and I may find another way natively in windows to resolve this as I don't want an external package to add in. – Codingale Apr 28 '15 at 12:30
  • actually txtFileName is a Text box Controll I just prefix them with txt :). anw if you find a way to accomplish what you need please post it here as an answer. – nayef harb Apr 28 '15 at 12:48