Skip to Content
0

How to open Folder Dialog Browser

Feb 13, 2017 at 11:27 AM

160

avatar image

How to realize function where when you pressed some user button control, folder browser dialog is open like on image. How basically get object(C#) folder browser dialog ?

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

1 Answer

Best Answer
DIEGO LOTHER Feb 13, 2017 at 03:07 PM
1

Hi Евгений,

A class to handle this functionalaty:

public enum DialogType
    {
        SAVE,
        OPEN,
        FOLDER
    };


    public class SelectFileDialog
    {
        private ManualResetEvent shutdownEvent = new ManualResetEvent(false);
        public string SelectedFile { get ; private set; }
        public string SelectedFolder { get ; private set; }


        private string folder, file, filter;
        private DialogType type;


        public SelectFileDialog(string folder, string file, string filter,
            DialogType type)
        {
            if (folder == null || file == null || filter == null)
                throw new ArgumentException(Messages.AllArgumentsMandatory);


            this.folder = folder;
            this.file = file;
            this.filter = filter;
            this.type = type;
        }


        private void InternalSelectFileDialog()
        {
            var form = new System.Windows.Forms.Form();


            form.TopMost = true;
            form.Height = 0;
            form.Width = 0;
            form.WindowState = FormWindowState.Minimized;
            form.Visible = true;


            switch (type)
            {
                case DialogType.FOLDER:
                    FolderDialog(form);
                    break;
                case DialogType.OPEN:
                    OpenDialog(form);
                    break;
                case DialogType.SAVE:
                    SaveDialog(form);
                    break;
            }
            shutdownEvent.Set();
        }


        private void FolderDialog(System.Windows.Forms.Form form)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();


            dialog.Description = Messages.FolderDialogTitle;
            dialog.RootFolder = Environment.SpecialFolder.MyComputer;
            //----------------------------------------------------------------//
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                form.Close();
                SelectedFolder = dialog.SelectedPath;
            }
            else
            {
                form.Close();
                SelectedFolder = "";
            }
        }


        private void OpenDialog(System.Windows.Forms.Form form)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            OpenOrSaveDialog(dialog, form);
        }


        private void SaveDialog(System.Windows.Forms.Form form)
        {
            SaveFileDialog dialog = new SaveFileDialog();
            OpenOrSaveDialog(dialog, form);
        }


        private void OpenOrSaveDialog(FileDialog dialog, System.Windows.Forms.Form form)
        {
            dialog.Title = Messages.FileDialogTitle;
            dialog.Filter = filter; //"TXT files (*.txt)|*.txt|All files (*.*)|*.*";
            dialog.InitialDirectory = folder;
            dialog.FileName = file;
            //----------------------------------------------------------------//
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                form.Close();
                SelectedFile = dialog.FileName;
            }
            else
            {
                form.Close();
                SelectedFile = "";
            }
        }


        public void Open()
        {
            Thread t = new Thread(new ThreadStart(this.InternalSelectFileDialog));
            t.SetApartmentState(ApartmentState.STA);
            t.Start();
            shutdownEvent.WaitOne();
        }
    }

A sample how to use this class:

            SelectFileDialog dialog = new SelectFileDialog("C:\\", "",
                "|*.xml", DialogType.SAVE);
            dialog.Open();
            if (!string.IsNullOrEmpty(dialog.SelectedFile))
            {

            }

This code was got from dover project: http://efpiva.github.io/

Hope it helps.

Kind Regards,

Diego Lother

Show 1 Share
10 |10000 characters needed characters left characters exceeded

Thank`s Diego. It worked !

0