|
using System;
using System.Windows.Forms;
namespace VVX
{
/// <summary>
/// Simple class to simplify access to OpenFileDialog and SaveFileDialog
/// WARNING: By no means complete!!!
/// </summary>
public static class FileDialog
{
/// <summary>
/// Type of file extension filters to list the File Open or Save dialog box
/// </summary>
public enum FileType
{
/// <summary>
/// Show the 'show me everything' extension, i.e., "*.*"
/// </summary>
All,
/// <summary>
/// Show common image file extensions such as
/// *.BMP, *.JPG, *.GIF, *.PNG"
/// </summary>
Image,
/// <summary>
/// Show common text file extensions such as *.TXT and *.CSV
/// </summary>
Text,
/// <summary>
/// Show *.XML file extension
/// </summary>
XML
}
/// <summary>
/// STATIC: Helper method returns string used for the Filters property of
/// the OpenFileDialog and the SaveFileDialog
/// </summary>
/// <param name="enFileType"></param>
/// <returns></returns>
public static string GetFilters(FileType enFileType)
{
string sRet = "";
switch (enFileType)
{
default:
case FileType.All:
break;
case FileType.Image:
sRet = "Image Files(*.BMP;*.JPG;*.GIF;*.PNG)|*.BMP;*.JPG;*.GIF;*.PNG";
break;
case FileType.Text :
sRet = "Text Files(*.TXT;*.CSV)|*.TXT;*.CSV";
break;
case FileType.XML:
sRet = "XML Files(*.XML)|*.XML";
break;
}
if (sRet.Length > 0)
sRet += "|";
sRet += "All files (*.*)|*.*";
return sRet;
}
/// <summary>
/// Displays an OpenFileDialog dialog box and returns the file selected by the user
/// </summary>
/// <param name="enFileType">FileType [enum] used to set the filters in the dialog box</param>
/// <returns>fully qualified file selected by the user</returns>
public static string GetFilenameToOpen(FileType enFileType)
{
return GetFilenameToOpen(enFileType, false, "");
}
/// <summary>
/// STATIC: Displays an OpenFileDialog dialog box and returns the file selected by the user
/// </summary>
/// <param name="enFileType">FileType [enum] used to set the filters in the dialog box</param>
/// <param name="sFolder">the folder in which to start looking</param>
/// <returns>fully qualified file selected by the user</returns>
public static string GetFilenameToOpen(FileType enFileType, string sFolder)
{
return GetFilenameToOpen(enFileType, false, sFolder);
}
/// <summary>
/// STATIC: Displays an OpenFileDialog dialog box and returns the file selected by the user
/// </summary>
/// <param name="enFileType">FileType [enum] used to set the filters in the dialog box</param>
/// <param name="bRestoreDirectory">if true, the previous 'current' folder will be preserved on exit from this dialog box</param>
/// <param name="sFolder">the folder in which to start looking</param>
/// <returns>fully qualified file selected by the user</returns>
public static string GetFilenameToOpen(FileType enFileType
, bool bRestoreDirectory
, string sFolder)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = GetFilters(enFileType);
dlg.RestoreDirectory = bRestoreDirectory;
if (sFolder.Length > 0)
dlg.InitialDirectory = sFolder;
if (dlg.ShowDialog() == DialogResult.OK)
{
return dlg.FileName;
}
return "";
}
/// <summary>
/// STATIC: Displays an SaveFileDialog dialog box and returns the file selected by the user
/// </summary>
/// <param name="enFileType">FileType [enum] used to set the filters in the dialog box</param>
/// <param name="sFile">the default name of the file</param>
/// <returns>fully qualified file selected by the user</returns>
public static string GetFilenameToSave(FileType enFileType, string sFile)
{
SaveFileDialog dlg = new SaveFileDialog();
dlg.Filter = GetFilters(enFileType);
dlg.FileName = sFile;
if (dlg.ShowDialog() == DialogResult.OK)
{
return dlg.FileName;
}
return "";
}
/// <summary>
/// Get the path to an application-related "standard" folder,
/// e.g., "Data" or "Images"
/// </summary>
/// <param name="sFolderName">if empty for app's folder, else a subfolder, e.g., Data</param>
/// <returns>fully qualified path to the named folder</returns>
public static string DoGetAppPath(string sFolderName) //e.g., Data
{
string sRetFolder = Application.ExecutablePath;
int n = sRetFolder.LastIndexOf(@"\");
// sFolderName is empty, then simply return the EXE's folder
if (n >= 0 && sFolderName.Length > 0)
{
sRetFolder = sRetFolder.Substring(0, n + 1);
sRetFolder = sRetFolder.Replace(@"bin\Release", sFolderName);
sRetFolder = sRetFolder.Replace(@"bin\Debug", sFolderName);
}
return sRetFolder;
}
}
}
|
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.