Provides a file chooser dialog - CSharp System.Windows.Forms

CSharp examples for System.Windows.Forms:Dialog

Description

Provides a file chooser dialog

Demo Code


using System.Windows.Forms;
using System.Threading.Tasks;
using System.Text;
using System.Linq;
using System.Diagnostics;
using System.Collections.Generic;
using System;//from  www. ja v  a2  s.c o  m

public class Main{
        /// <summary>
        /// Provides a file chooser
        /// </summary>
        /// <returns>the path to the file chosen or null for no file selected</returns>
        public static string ChooseFile()
        {
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            dlg.FileName = "Document"; // Default file name
            dlg.DefaultExt = ".txt"; // Default file extension
            dlg.Filter = "Protocol (.txt)|*.txt"; // Filter files by extension

            // Show open file dialog box
            Nullable<bool> result = dlg.ShowDialog();

            // Process open file dialog box results
            if (result == true)
            {
                // Open document
                string filename = dlg.FileName;
                return filename;
            }
            return null;
        }
}

Related Tutorials