using System; using System.Windows.Forms; namespace QiHe.CodeLib { /// /// Represents the file type. /// public enum FileType { /// /// Txt file /// Txt, /// /// Rtf file /// Rtf, /// /// Html file /// Html, /// /// Xml file /// Xml, /// /// PDF file /// PDF, /// /// Bin file /// Bin, /// /// Zip file /// Zip, /// /// Image file /// Img, /// /// Excel 97 xls /// Excel97, /// /// Excel 2007 Open Xml Format /// Excel2007, /// /// All file type /// All } /// /// FileSelector /// public static class FileSelector { /// /// The Title of FileDialog for select a Single File /// public static string TitleSingleFile = "Please choose a file"; /// /// The Title of FileDialog for select Multiple Files /// public static string TitleMultiFile = "Please choose files"; /// /// Filter /// public static string Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"; /// /// Sets the file extension. /// /// The file extension. public static FileType FileExtension { set { switch (value) { case FileType.Txt: Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"; break; case FileType.Rtf: Filter = "Rtf files (*.rtf)|*.rtf|All files (*.*)|*.*"; break; case FileType.Html: Filter = "Html files (*.htm;*.html)|*.htm;*.html|All files (*.*)|*.*"; break; case FileType.Xml: Filter = "XML files (*.xml)|*.xml|Config files (*.config)|*.config|All files (*.*)|*.*"; break; case FileType.PDF: Filter = "PDF files (*.pdf)|*.pdf|PDF form files (*.fdf)|*.fdf|All files (*.*)|*.*"; break; case FileType.Bin: Filter = "Application files(*.exe;*.dll)|*.exe;*.dll|Binary files (*.bin)|*.bin|All files (*.*)|*.*"; break; case FileType.Zip: Filter = "Zip files (*.zip)|*.zip|All files (*.*)|*.*"; break; case FileType.Img: Filter = "Gif(*.gif)|*.gif|Jpeg(*.jpg)|*.jpg|Emf(*.emf)|*.emf|Bmp(*.bmp)|*.bmp|Png(*.png)|*.png"; break; case FileType.Excel97: Filter = "Excel files (*.xls)|*.xls|All files (*.*)|*.*"; break; case FileType.Excel2007: Filter = "Excel files (*.xlsx)|*.xlsx|All files (*.*)|*.*"; break; case FileType.All: Filter = "All files (*.*)|*.*"; break; } } } /// /// Gets the image format. /// /// The image format. public static System.Drawing.Imaging.ImageFormat ImageFormat { get { switch (SFD.FilterIndex) { case 1: return System.Drawing.Imaging.ImageFormat.Gif; case 2: return System.Drawing.Imaging.ImageFormat.Jpeg; case 3: return System.Drawing.Imaging.ImageFormat.Emf; case 4: return System.Drawing.Imaging.ImageFormat.Bmp; case 5: return System.Drawing.Imaging.ImageFormat.Png; default: return System.Drawing.Imaging.ImageFormat.Png; } } } /// /// /// public static OpenFileDialog OFD = new OpenFileDialog(); /// /// /// public static SaveFileDialog SFD = new SaveFileDialog(); /// /// Gets or sets the initial directory displayed by the file dialog box. /// public static string InitialPath { get { return OFD.InitialDirectory; } set { OFD.InitialDirectory = value; SFD.InitialDirectory = value; } } public static string FileName { get { return OFD.FileName; } set { OFD.FileName = value; SFD.FileName = value; } } /// /// Initializes the class. /// static FileSelector() { OFD.RestoreDirectory = false; } /// /// Browses the file. /// /// public static string BrowseFile() { OFD.Title = TitleSingleFile; OFD.Filter = Filter; OFD.Multiselect = false; if (OFD.ShowDialog() == DialogResult.OK) { return OFD.FileName; } else { return null; } } /// /// Browses the files. /// /// public static string[] BrowseFiles() { OFD.Title = TitleMultiFile; OFD.Filter = Filter; OFD.Multiselect = true; if (OFD.ShowDialog() == DialogResult.OK) { return OFD.FileNames; } else { return null; } } /// /// Browses the file for save. /// /// public static string BrowseFileForSave() { SFD.Title = TitleSingleFile; SFD.Filter = Filter; if (SFD.ShowDialog() == DialogResult.OK) { return SFD.FileName; } else { return null; } } /// /// Browses the file. /// /// The type. /// public static string BrowseFile(FileType type) { FileExtension = type; return BrowseFile(); } /// /// Browses the file. /// /// The filter. /// public static string BrowseFile(string filter) { Filter = filter; return BrowseFile(); } /// /// Browses the files. /// /// The type. /// public static string[] BrowseFiles(FileType type) { FileExtension = type; return BrowseFiles(); } /// /// Browses the files. /// /// The filter. /// public static string[] BrowseFiles(string filter) { Filter = filter; return BrowseFiles(); } /// /// Browses the file for save. /// /// The type. /// public static string BrowseFileForSave(FileType type) { FileExtension = type; return BrowseFileForSave(); } /// /// Browses the file for save. /// /// The filter. /// public static string BrowseFileForSave(string filter) { Filter = filter; return BrowseFileForSave(); } } }