Helper class to facilitate Common operations on file dialogs

This article provides a way to segregate the handling of file dialogs that we most commonly use

The article is self explanatory. However necessary comments are provided.

How to use:

string xpsDataFile = ExtendedDialogs.GetFileNameForSave(FileExtenion.XPS, DefaultFileName);

string csvDataFile = ExtendedDialogs.GetFileNameForSave(FileExtenion.CSV, DefaultFileName);

string excelDataFile = ExtendedDialogs.GetFileNameForSave(FileExtenion.XLS, DefaultFileName);

Default file name handles the "file name" that appears in by default in the save dialogs.

Helper class

#region
Namespaces

 

using Microsoft.Win32;

 

#endregion Namespaces

 

Namespace UI.Common.Dialogs

{

    /// <summary>

    /// FileExtensions.

    /// </summary>

    public enum FileExtenion

    {

        CSV     = 0,

        XLS     = 1,

        PDF     = 2,

        XML     = 3,

        XPS     = 4

       

    }

 

    /// <summary>

    /// Extended Dialogs.

    /// </summary>

    public static class ExtendedDialogs

    {

        /// <summary>

        /// Gets a file name to save.

        /// </summary>

        /// <returns></returns>

        public static string GetFileNameForSave(FileExtenion fileExtension, string fileName)

        {

            var dialog = new SaveFileDialog

                             {

                                 AddExtension   = true,

                                 DefaultExt     = string.Concat(".",fileExtension.ToString()),

                                 FileName       = fileName

                             };

 

            var fileExtensionDescription    = GetFileExtensionDescription(fileExtension);

            dialog.Title                    = LocalizationManager.GetValue(ResourceKeyNames.COMMON_MESSAGE_APPLICATION);

            dialog.Filter                   = string.Format("{0} (*.{1})|", fileExtensionDescription, fileExtension);

            dialog.ValidateNames            = false;

           

            //GetValueOrDefault

            return dialog.ShowDialog().Value ? string.Format("{0}.{1}", dialog.FileName, dialog.DefaultExt) : string.Empty;

        }

 

        /// <summary>

        /// Gets a file name to open.

        /// </summary>

        /// <returns></returns>

        public static string GetFileNameForOpen(FileExtenion fileExtension, string fileName)

        {

            var dialog = new OpenFileDialog()

            {

                AddExtension = true,

                DefaultExt = string.Concat(".", fileExtension.ToString()),

                FileName = fileName

            };

 

            var fileExtensionDescription = GetFileExtensionDescription(fileExtension);

            dialog.Title                 = LocalizationManager.GetValue(ResourceKeyNames.COMMON_MESSAGE_APPLICATION);

            //saveDlg.Filter = "XPS documents|*.xps";

            dialog.Filter                = string.Format("{0}|(*.{1})", fileExtensionDescription, fileExtension.ToString().ToLower());

            dialog.ValidateNames        = false;

 

            return dialog.ShowDialog().Value ? string.Format("{0}.{1}", dialog.FileName, dialog.DefaultExt) : string.Empty;

        }

 

        /// <summary>

        /// Gets the file extension description.

        /// </summary>

        /// <param name="fileExtension"></param>

        /// <returns></returns>

        private static string GetFileExtensionDescription(FileExtenion fileExtension)

        {

            string fileExtensionDescription;

 

            switch (fileExtension)

            {

                case FileExtenion.CSV:

                    fileExtensionDescription = "CSV (Comma delimited)";

                    break;

                case FileExtenion.XLS:

                    fileExtensionDescription = "Excel 97-2003 Workbook";

                    break;

                case FileExtenion.PDF:

                    fileExtensionDescription = "PDF documents";

                    break;

                case FileExtenion.XML:

                    fileExtensionDescription = "XML Files";

                    break;

                case FileExtenion.XPS:

                    fileExtensionDescription = "XPS documents";

                    break;

                 default:

                    fileExtensionDescription = "CSV (Comma delimited)";

                    break;

            } //end switch

 

            return fileExtensionDescription;

        }

    }

}

 

By [)ia6l0 iii   Popularity  (987 Views)