We help IT Professionals succeed at work.

File and Folder Selection DialogBox

Kalpesh Chhatrala
on
Hi,

I want to select file and folder (both at a time) using C#.

please help me

thanks.
Comment
Watch Question

Éric MoreauSenior .Net Consultant
Top Expert 2016

Commented:

Commented:
include a "using System.IO" and use Path from it - quick example:

            OpenFileDialog ofd = new OpenFileDialog();
            DialogResult dr = ofd.ShowDialog();
            string filename = "";
            string directory = "";
            if (dr == DialogResult.OK)
            {
                filename = Path.GetFileName(ofd.FileName);
                directory = Path.GetDirectoryName(ofd.FileName);
            }
Kalpesh ChhatralaSoftware Consultant

Author

Commented:
i am familiar with OpenFileDialog  class.

I want to select File and Folder from Dialog box. OpenFileDialog box not allowed to select folder.
Éric MoreauSenior .Net Consultant
Top Expert 2016

Commented:
I am not sure to understand. The OpenFileDialog let you pick a folder and then a file from that folder no? See the attached image.
OpenFileDialog.PNG
Commented:
Understood.  By default, the selection of files necessitates the navigation of folders.  Therefore, the user would need to indicate, somehow, that they are wanting to select a folder and not navigate down into it to find a file.  I'm guessing you want this functionality so you would not have to use the (non-inheritable) standard dialog classes for selecting files and folders independently.

For this kind of solution, you probably would want to implement a tree view and fill it with folders and files much like "Windows Explorer" would do.  You would then handle the click event, where double-click would navigate into the folder but ctrl+single click would include the folder in the selection, for example.  https://msdn.microsoft.com/en-us/library/ms171645.aspx may be what you are looking for?  That walk through sounds like what you are looking for.
Kalpesh ChhatralaSoftware Consultant

Author

Commented:
thanks d_york.

i understand i need to design this type of dialog box.

do you know this type of dialogbox (file and folder select functionality) available in free ?

thanks for reply.
Éric MoreauSenior .Net Consultant
Top Expert 2016

Commented:
So the file you select is not related to the selected folder. Right?

This is a specific need and you will need to create your own dialog.

I have some code that creates the folders structure in a treeview: http://emoreau.com/Entries/Articles/2006/03/Folders-synchronization-using-the-SystemIO-namespace.aspx

Commented:
I do not know of any free control that provides that functionality.
Éric MoreauSenior .Net Consultant
Top Expert 2016

Commented:
that suggested comment is not providing a solution for this question!

the solution (building a custom dialog to show folders) is available from https:#40680329