using System; using System.Windows.Forms; using WinForms = System.Windows.Forms; namespace VmWrapperExpress.Services { public static class CommonDialogs { #region File Dialog Methods /// <summary> /// Shows an Open File common dialog. /// </summary> /// <param name="dialogCaption">The dialog caption.</param> /// <param name="fileFilter">The file filter to use.</param> /// <returns>A path to the file selected by the user.</returns> public static string GetFileToOpen(string dialogCaption, string fileFilter) { // Show Open File dialog var dialog = new OpenFileDialog(); dialog.Title = dialogCaption; dialog.InitialDirectory = Environment.SpecialFolder.MyDocuments.ToString(); dialog.Filter = fileFilter; dialog.FilterIndex = 1; var result = dialog.ShowDialog(); // Get the file name var filePath = String.Empty; if (result == DialogResult.OK) { filePath = dialog.FileName; } // Close and dispose dialog.Dispose(); // Set return value return filePath; } /// <summary> /// Shows a Save File common dialog. /// </summary> /// <param name="dialogCaption">The dialog caption.</param> /// <param name="fileFilter">The file filter to use.</param> /// <returns>A path to the save location selected by the user.</returns> public static string GetFileToSave(string dialogCaption, string fileFilter) { // Show Save File dialog var dialog = new SaveFileDialog(); dialog.Title = "dialogCaption"; dialog.InitialDirectory = Environment.SpecialFolder.MyDocuments.ToString(); dialog.Filter = fileFilter; dialog.FilterIndex = 1; var result = dialog.ShowDialog(); // Set file name var filePath = String.Empty; if (result == DialogResult.OK) { filePath = dialog.FileName; } // Close and dispose dialog.Dispose(); // Set return value return filePath; } /// <summary> /// Displays a Folder Browser dialog. /// </summary> /// <param name="prompt">The prompt to display to the user.</param> /// <param name="showNewFolderButton">Whether to show the 'New Folder' button.</param> /// <returns>A path to the folder selected.</returns> public static string GetFolderPath(string prompt, bool showNewFolderButton) { // Initialize var folderBrowser = new FolderBrowserDialog(); var folderPath = String.Empty; // Initialize Folder Browser dialog folderBrowser.Description = prompt; folderBrowser.ShowNewFolderButton = showNewFolderButton; // Execute Folder Browser dialog var result = folderBrowser.ShowDialog(); if (result == DialogResult.OK) { folderPath = folderBrowser.SelectedPath; } // Close and dispose folderBrowser.Dispose(); // Set return value return folderPath; } #endregion /* Note that this class uses the WinForms versions of .NET's common dialog * wrappers. That's because the WinForms versions are Vista-aware, whereas * the WPF versions are not. */ } }
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)