namespace RobvanderWoude { [System.Runtime.InteropServices.Guid( "4CB376E6-5238-4033-9EFB-18972527028C" )] public interface SaveFileBox_Interface { [System.Runtime.InteropServices.DispId( 1 )] void CheckUpdate( ); [System.Runtime.InteropServices.DispId( 2 )] string Credits( ); [System.Runtime.InteropServices.DispId( 3 )] string Help( int html = 0 ); [System.Runtime.InteropServices.DispId( 4 )] string ListProperties( ); [System.Runtime.InteropServices.DispId( 5 )] string SampleCode( ); [System.Runtime.InteropServices.DispId( 6 )] void Show( ); } [System.Runtime.InteropServices.Guid( "3A5B9C48-7956-4B36-9301-0F98F1B3D067" ), System.Runtime.InteropServices.InterfaceType( System.Runtime.InteropServices.ComInterfaceType.InterfaceIsIDispatch )] public interface SaveFileBox_Events { } [System.Runtime.InteropServices.Guid( "128D27C4-4387-4FFC-9F7E-95385D1CF6E6" ), System.Runtime.InteropServices.ClassInterface( System.Runtime.InteropServices.ClassInterfaceType.AutoDual ), System.Runtime.InteropServices.ComSourceInterfaces( typeof( SaveFileBox_Events ) )] public class SaveFileBox : SaveFileBox_Interface { #region Default Values const string defaultdescription = "Specify a file"; const string defaultfilter = "All files (*.*)|*.*"; static readonly string defaultstartfolder = System.IO.Directory.GetCurrentDirectory( ); static readonly string defaulttitle = string.Format( "{0}, Version {1}", Global.Common.ProgramInfo.FileName, Global.Common.ProgramInfo.FileVersion ); #endregion Default Values #region Methods public void CheckUpdate( ) { Global.Common.ProgramInfo.CheckUpdate( ); } public string Credits( ) { return Global.Common.Credits( ); } public string Help( int html = 0 ) { int col1width = 0; int col2width = 0; int col3width = 0; int col4width = 0; int col5width = 0; string help = string.Empty; string linetemplate = string.Empty; string separatorline = string.Empty; if ( html == 1 ) { help += "

Help for OpenFileBox class

\n\n"; help += "

Present an Open File dialog, and return the selected file path

\n\n"; help += "

COM ProgID: RobvanderWoude.OpenFileBox

\n\n"; } else { help += "Help for OpenFileBox class\n"; help += new string( '\u2500', help.Length ) + "\n\n"; help += "Present an Open File dialog, and return the selected file path\n\n\n"; help += "COM ProgID: RobvanderWoude.OpenFileBox\n\n\n"; } #region Properties if ( html == 1 ) { separatorline = string.Empty; linetemplate = "\n\t{0}\n\t{1}\n\t{2}\n\t{3}\n\t{4}\n\n"; help += "\n"; help += "\n"; help += "\t\n"; help += "\n"; help += "\n"; help += "\t\n"; help += "\t\n"; help += "\t\n"; help += "\t\n"; help += "\t\n"; help += "\n"; } else { col1width = 16; col2width = 50; col3width = 9; col4width = 9; col5width = 44; separatorline = "\u251C" + new string( '\u2500', 2 + col1width ) + "\u253C" + new string( '\u2500', 2 + col2width ) + "\u253C" + new string( '\u2500', 2 + col3width ) + "\u253C" + new string( '\u2500', 2 + col4width ) + "\u253C" + new string( '\u2500', 2 + col5width ) + "\u2524\n"; linetemplate = "\u2502 {0,-" + col1width.ToString( ) + "} \u2502 {1,-" + col2width.ToString( ) + "} \u2502 {2,-" + col3width.ToString( ) + "} \u2502 {3,-" + col4width.ToString( ) + "} \u2502 {4,-" + col5width.ToString( ) + "} \u2502\n"; help += "\u250C" + new string( '\u2500', 14 + col1width + col2width + col3width + col4width + col5width ) + "\u2510\n"; help += "\u2502" + new string( ' ', 14 + col1width + col2width + col3width + col4width + col5width ) + "\u2502\n"; help += string.Format( "\u2502 {0,-" + ( 12 + col1width + col2width + col3width + col4width + col5width ).ToString( ) + "} \u2502\n", "Properties (Note that all properties are string or integer, no booleans, objects or arrays)" ); help += "\u2502" + new string( ' ', 14 + col1width + col2width + col3width + col4width + col5width ) + "\u2502\n"; help += "\u255E" + new string( '\u2550', 2 + col1width ) + "\u2564" + new string( '\u2550', 2 + col2width ) + "\u2564" + new string( '\u2550', 2 + col3width ) + "\u2564" + new string( '\u2550', 2 + col4width ) + "\u2564" + new string( '\u2550', 2 + col5width ) + "\u2561\n"; help += string.Format( linetemplate, "Property Name", "Description", "Mandatory", "Read-Only", "Default (Allowed) Values" ); help += "\u255E" + new string( '\u2550', 2 + col1width ) + "\u256A" + new string( '\u2550', 2 + col2width ) + "\u256A" + new string( '\u2550', 2 + col3width ) + "\u256A" + new string( '\u2550', 2 + col4width ) + "\u256A" + new string( '\u2550', 2 + col5width ) + "\u2561\n"; } help += HelpTableRow( linetemplate, "addallfiles", "Add \"All files (*.*)\" to filter (0=false; 1=true)", "no", "no", "1", html ); help += separatorline; help += HelpTableRow( linetemplate, "addextension", "Add extension if user forgot it (0=false; 1=true)", "no", "no", "1", html ); help += separatorline; help += HelpTableRow( linetemplate, "debuginfo", Global.Common.Help.debuginfo, "N/A", "YES", "", html ); help += separatorline; help += HelpTableRow( linetemplate, "defaultextension", "Default extension to be added", "no", "no", "", html ); help += separatorline; help += HelpTableRow( linetemplate, "description", "Text above directory tree", "no", "no", "", html ); help += separatorline; help += HelpTableRow( linetemplate, "errors", Global.Common.Help.errors, "N/A", "YES", "", html ); help += separatorline; help += HelpTableRow( linetemplate, "filter", "File filter", "no", "no", "\"All files (*.*)|*.*\"", html ); help += separatorline; help += HelpTableRow( linetemplate, "forceext", "Append default extension if missing", "", html ); help += separatorline; help += HelpTableRow( linetemplate, "multidotted", "Allow multiple extensions (0=false; 1=true)", "no", "no", "1", html ); help += separatorline; help += HelpTableRow( linetemplate, "overwrite", "Overwrite file if it exists (0=false; 1=true)", "no", "no", "0", html ); help += separatorline; help += HelpTableRow( linetemplate, "selectedfile", "The path of the selected file(s)", "N/A", "YES", "", html ); help += separatorline; help += HelpTableRow( linetemplate, "startfolder", "The initial folder for the dialog", "no", "no", "", html ); help += separatorline; help += HelpTableRow( linetemplate, "title", Global.Common.Help.title, "no", "no", defaulttitle, html ); help += separatorline; help += HelpTableRow( linetemplate, "verbose", "Prompt if file does not exist (0=false; 1=true)", "no", "no", "0", html ); help += separatorline; help += HelpTableRow( linetemplate, "Version", Global.Common.Help.version, "N/A", "YES", "", html ); if ( html == 1 ) { help += "
Properties (Note that all properties are string or integer, no booleans, objects or arrays)
Property NameDescriptionMandatoryRead-OnlyDefault (Allowed) Values
\n\n\n"; } else { help += "\u2514" + new string( '\u2500', 2 + col1width ) + "\u2534" + new string( '\u2500', 2 + col2width ) + "\u2534" + new string( '\u2500', 2 + col3width ) + "\u2534" + new string( '\u2500', 2 + col4width ) + "\u2534" + new string( '\u2500', 2 + col5width ) + "\u2518\n\n\n"; } #endregion Properties #region Methods if ( html == 1 ) { separatorline = string.Empty; linetemplate = "\n\t{0}\n\t{1}\n\t{2}\n\n"; help += "\n"; help += "\n"; help += "\t\n"; help += "\n"; help += "\n"; help += "\t\n"; help += "\t\n"; help += "\t\n"; help += "\n"; } else { col1width = 18; col2width = 74; col3width = 42; separatorline = "\u251C" + new string( '\u2500', 2 + col1width ) + "\u253C" + new string( '\u2500', 2 + col2width ) + "\u253C" + new string( '\u2500', 2 + col3width ) + "\u2524\n"; linetemplate = "\u2502 {0,-" + col1width.ToString( ) + "} \u2502 {1,-" + col2width.ToString( ) + "} \u2502 {2,-" + col3width.ToString( ) + "} \u2502\n"; help += "\u250C" + new string( '\u2500', 8 + col1width + col2width + col3width ) + "\u2510\n"; help += "\u2502" + new string( ' ', 8 + col1width + col2width + col3width ) + "\u2502\n"; help += string.Format( "\u2502 {0,-" + ( 6 + col1width + col2width + col3width ).ToString( ) + "} \u2502\n", "Methods" ); help += "\u2502" + new string( ' ', 8 + col1width + col2width + col3width ) + "\u2502\n"; help += "\u255E" + new string( '\u2550', 2 + col1width ) + "\u2564" + new string( '\u2550', 2 + col2width ) + "\u2564" + new string( '\u2550', 2 + col3width ) + "\u2561\n"; help += string.Format( linetemplate, "Method Name", "Description", "Requirements" ); help += "\u255E" + new string( '\u2550', 2 + col1width ) + "\u256A" + new string( '\u2550', 2 + col2width ) + "\u256A" + new string( '\u2550', 2 + col3width ) + "\u2561\n"; } help += HelpTableRow( linetemplate, "CheckUpdate", Global.Common.Help._checkupdate, "", html ); help += separatorline; help += HelpTableRow( linetemplate, "Credits", Global.Common.Help._credits, "", html ); help += separatorline; help += HelpTableRow( linetemplate, "Help", Global.Common.Help._help, "", html ); help += separatorline; help += HelpTableRow( linetemplate, "ListProperties", Global.Common.Help._listproperties, "", html ); help += separatorline; help += HelpTableRow( linetemplate, "SampleCode", Global.Common.Help._samplecode, "", html ); help += separatorline; help += HelpTableRow( linetemplate, "Show", "Presents an Open File dialog based on the current property values, and if \"OK\"is clicked, saves the selected file path in the \"selectedfile\" property (if \"Cancel\" is clicked, \"selectedfile\" will be empty)", "", html ); if ( html == 1 ) { help += "
Methods
Method NameDescriptionRequirements
\n\n\n"; } else { help += "\u2514" + new string( '\u2500', 2 + col1width ) + "\u2534" + new string( '\u2500', 2 + col2width ) + "\u2534" + new string( '\u2500', 2 + col3width ) + "\u2518\n\n\n"; } #endregion Methods #region Example if ( html == 1 ) { help += "

VBScript usage example:

\n\n
";
			}
			else
			{
				help += "VBScript usage example:\n";
				help += new string( '\u2500', 23 ) + "\n\n";
			}

			help += SampleCode( );

			if ( html == 1 )
			{
				help += "
"; } help += "\n\n"; #endregion Example help = help.Replace( "\n", System.Environment.NewLine ); return help; } private string HelpTableRow( string template, string col1text, string col2text, string col3text, int html ) { if ( html == 1 ) { return Global.Common.Help.HelpTableRowHTML( col1text, col2text, col3text ); } else { return Global.Common.Help.HelpTableRowText( template, col1text, col2text, col3text, "", "" ); } } private string HelpTableRow( string template, string col1text, string col2text, string col3text, string col4text, string col5text, int html ) { if ( html == 1 ) { return Global.Common.Help.HelpTableRowHTML( col1text, col2text, col3text, col4text, col5text ); } else { return Global.Common.Help.HelpTableRowText( template, col1text, col2text, col3text, col4text, col5text ); } } public string ListProperties( ) { return Global.Common.Lists.Properties( this ); } public string SampleCode( ) { string code = "Set objSaveFileBox = CreateObject( \"RobvanderWoude.SaveFileBox\" )\n\n"; code += "With objSaveFileBox\n"; code += "\t.startfolder = \"D:\\\"\n"; code += "\t.Show\n"; code += "\tWScript.Echo \"Selected target file: \" & .selectedfile\n"; code += "\tWScript.Echo\n"; code += "\tWScript.Echo .ListProperties( )\n"; code += "End With\n\n"; code += "Set objSaveFileBox = Nothing"; return code; } [System.STAThread] public void Show( ) { #region Apply Settings string ext = System.Text.RegularExpressions.Regex.Match( filter, @"(\.[^.]+)$", System.Text.RegularExpressions.RegexOptions.IgnoreCase ).Groups[0].ToString( ); if ( string.IsNullOrWhiteSpace( ext ) ) { _errors.Add( Global.Common.TimeStamp( ) + "Invalid filetype specification" ); } // If only "*.ext" is specified, use "ext files (*.ext)|*.ext" instead if ( System.Text.RegularExpressions.Regex.IsMatch( filter, @"^\*\.(\*|\w+)$" ) ) { if ( ext == ".*" ) { filter = string.Format( "All files (*.{0})|*.{0}", ext ); } else { filter = string.Format( "{0} files (*.{0})|*.{0}", ext ); } } // Use "All files" filter if not specified if ( string.IsNullOrWhiteSpace( filter ) ) { filter = "All files (*.*)|*.*"; } #endregion Apply Settings try { using ( System.Windows.Forms.SaveFileDialog dialog = new System.Windows.Forms.SaveFileDialog( ) ) { dialog.AddExtension = _forceext; dialog.CheckFileExists = false; dialog.CheckPathExists = !_overwrite; dialog.CreatePrompt = _verbose; dialog.DefaultExt = defaultextension; dialog.Filter = filter; dialog.FilterIndex = 1; dialog.InitialDirectory = startfolder; dialog.OverwritePrompt = !_overwrite; dialog.RestoreDirectory = true; dialog.SupportMultiDottedExtensions = _multidotted; dialog.Title = title; dialog.ValidateNames = true; if ( dialog.ShowDialog( ) == System.Windows.Forms.DialogResult.OK ) { _selectedfile = dialog.FileName; } else { // Canceled _selectedfile = string.Empty; } } } catch ( System.Exception e ) { _errors.Add( Global.Common.TimeStamp( ) + "Error while trying to show Save File dialog: " + e.Message ); } } #endregion Methods #region Properties private bool _addallfiles = true; [System.Runtime.InteropServices.ComVisible( true )] public int addallfiles { get { return ( _addallfiles ? 1 : 0 ); } set { _addallfiles = ( value == 1 ); } } private bool _addextension = false; [System.Runtime.InteropServices.ComVisible( true )] public int addextension { get { return ( _addextension ? 1 : 0 ); } set { _addextension = ( value == 1 ); } } private System.Collections.Generic.List _debuginfo = new System.Collections.Generic.List( ); [System.Runtime.InteropServices.ComVisible( true )] public string debuginfo { get { return string.Join( "\n", _debuginfo.ToArray( ) ); } } private string _defaultextension = string.Empty; [System.Runtime.InteropServices.ComVisible( true )] public string defaultextension { get { return _defaultextension; } set { if ( value.StartsWith( "." ) ) { _defaultextension = value.Trim( ); } else { _debuginfo.Add( timestamp + "Invalid default extension, using \".*\" instead" ); _defaultextension = ".*"; } } } private string _description = defaultdescription; [System.Runtime.InteropServices.ComVisible( true )] public string description { get { return _description; } set { _description = value.Trim( ); } } private System.Collections.Generic.List _errors = new System.Collections.Generic.List( ); [System.Runtime.InteropServices.ComVisible( true )] public string errors { get { return string.Join( "\n", _errors.ToArray( ) ); } } private string _filter = defaultfilter; [System.Runtime.InteropServices.ComVisible( true )] public string filter { get { return _filter; } set { // If only "*.ext" is specified, use "ext files (*.ext)|*.ext" instead if ( System.Text.RegularExpressions.Regex.IsMatch( value, @"^\*\.(\*|\w+)$" ) ) { string ext = value.Substring( 2 ).ToLower( ); if ( ext == ".*" ) { _filter = string.Format( "All files (*.{0})|*.{0}", ext ); } else { _filter = string.Format( "{0} files (*.{0})|*.{0}", ext ); } } else { _filter = value; } if ( _addallfiles ) { // Append "All files" filter if not specified if ( !System.Text.RegularExpressions.Regex.IsMatch( filter, @"All files\s+\(\*\.\*\)\|\*\.\*", System.Text.RegularExpressions.RegexOptions.IgnoreCase ) ) { if ( string.IsNullOrWhiteSpace( _filter ) ) { _filter = "All files (*.*)|*.*"; } else { _filter += "|All files (*.*)|*.*"; } } } } } private bool _forceext = true; [System.Runtime.InteropServices.ComVisible( true )] public int forceext { get { return ( _forceext ? 1 : 0 ); } set { _forceext = ( value == 1 ); } } private bool _multidotted = true; [System.Runtime.InteropServices.ComVisible( true )] public int multidotted { get { return ( _multidotted ? 1 : 0 ); } set { _multidotted = ( value == 1 ); } } private bool _overwrite = false; [System.Runtime.InteropServices.ComVisible( true )] public int overwrite { get { return ( _overwrite ? 1 : 0 ); } set { _overwrite = ( value == 1 ); } } private string _selectedfile = string.Empty; [System.Runtime.InteropServices.ComVisible( true )] public string selectedfile { get { return _selectedfile; } } private string _startfolder = defaultstartfolder; [System.Runtime.InteropServices.ComVisible( true )] public string startfolder { get { return _startfolder; } set { if ( System.IO.Directory.Exists( value ) ) { try { _startfolder = System.IO.Path.GetFullPath( value ); } catch ( System.ArgumentException ) { try { // Assuming the error is caused by a trailing backslash in doublequotes _startfolder = System.IO.Path.GetFullPath( value.Substring( 0, startfolder.IndexOf( '"' ) ) + "." ); } catch ( System.Exception ) { _debuginfo.Add( timestamp + "Invalid start directory, using current directory instead" ); _startfolder = "."; } } } else { _debuginfo.Add( timestamp + "Invalid start directory, using current directory instead" ); _startfolder = "."; } _selectedfile = _startfolder; } } public static string timestamp => Global.Common.TimeStamp( ); private string _title = defaulttitle; [System.Runtime.InteropServices.ComVisible( true )] public string title { get { return _title; } set { _title = value.Trim( ); } } private bool _verbose = false; [System.Runtime.InteropServices.ComVisible( true )] public int verbose { get { return ( _verbose ? 1 : 0 ); } set { _verbose = ( value == 1 ); } } [System.Runtime.InteropServices.ComVisible( true )] public string Version { get { return Global.Common.ProgramInfo.FileVersion; } } #endregion Properties } }