Hi guys,

How to show certain files according the selected extension in openFileDialog() object???

Thanks

[EL-Prince]

Recommended Answers

All 7 Replies

first thanks DdoubleD for your reply, but this's not what I want.
When I change the filter/extension of openFileDialog such as "All files" to ".txt" the dialog should showing any .txt files only, but that's not what happens.

So, how could I achieve this???

Thanks
[EL-Prince]

You will always see the directories in the directory you are in with OpenFileDialog. And if you don't see any *.txt files that could be because there are no *.txt files in that specific directory.

So, do you know how to display files only without showing directories ???

Thanks

[EL-Prince]

Its not possible with the OpenFileDialog. You would probably need to roll your own control. A simple form with a listbox would do(see attached image).
Code:

public partial class FileDialog : Form
    {
        public FileDialog()
        {
            InitializeComponent();
        }

        private void FileDialog_Load(object sender, EventArgs e)
        {
            //directory to retrieve files from
            const string  DirPath = @"C:\Users\Dan\Documents";

            //get files
            string[] files = Directory.GetFiles(DirPath, "*.txt", SearchOption.TopDirectoryOnly);
            
            //display filenames in listbox
            foreach (string file in files)
            {
                ListFile lf = new ListFile { Path = file };
                lstFiles.Items.Add(lf);
            }
        }

        //struct to store file in listbox
        private struct ListFile
        {
            //path store full file path
            public string Path {get;set;}

            //to string returns just file name for display
            public override string ToString()
            {
                return Path.Substring(Path.LastIndexOf(@"\") + 1);
            }
        }

        //store selected file path
        public string selectedFile { get; set; }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            //if file selected
            if (lstFiles.SelectedIndex >= 0)
            {
                //store path
                selectedFile = ((ListFile)lstFiles.SelectedItem).Path;

                //Set dialog result and hide form
                this.DialogResult = DialogResult.OK;
                this.Hide();
            }
            else
            {
                MessageBox.Show("Please select a file", "No File Selected");
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            //set empty path and dialog result
            selectedFile = string.Empty;
            this.DialogResult = DialogResult.Cancel;
        }
    }

How to use:

FileDialog dialog = new FileDialog();
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                MessageBox.Show("Selected File: " + dialog.selectedFile);
            }
            dialog.Dispose();
commented: Great! Remembers me of my old programming days in Modula-2, where I did exactly the same! +6

Thanks, guys I figured it out

Thanks, guys I figured it out

It would be great if you could give the solution here.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.