Try File Path with OpenFileDialog - CSharp System.Windows.Forms

CSharp examples for System.Windows.Forms:Dialog

Description

Try File Path with OpenFileDialog

Demo Code


using System.Windows.Forms;
using System.Windows;
using System.Linq;
using System.Collections.Generic;

public class Main{
        public static bool TryFilePath(string dialogTitle, string filter, out string fileName)
        {/*from ww w  . ja v a 2s. c o  m*/
            var fileSelector = new OpenFileDialog()
            {
                Title = dialogTitle,
                Filter = filter,
                Multiselect = false,
                CheckFileExists = true,
                DefaultExt = filter.Split('|').ElementAt(1).Replace("*", "") // It SHOULD be an extension method, so todo
            };
            if (fileSelector.ShowDialog() == DialogResult.OK)
            {
                fileName = fileSelector.FileName;
                return true;
            }
            else
            {
                fileName = string.Empty;
                return false;
            }
        }
}

Related Tutorials