@@ -819,9 +819,11 @@ public void ImportDatabase(string fileToImport, string exportPath)
819819 OnDatabaseImported ? . Invoke ( this , new OnDatabaseImportedEventArgs ( fileUnzipFullName , DateTime . Now ) ) ;
820820 }
821821 /// <summary>
822- /// Exports a group of database files to a single zipped file.
822+ /// Exports a group of database files to a single zipped file.
823823 /// </summary>
824- /// <param name="filename"></param>
824+ /// <param name="pathToSave">A folder where the database file will be created.</param>
825+ /// <param name="filename">A name for the database file.</param>
826+ /// <param name="fileExtension">An extension for the file.</param>
825827 public void ExportDatabase ( string pathToSave , string filename , string fileExtension = ".db" )
826828 {
827829 //Checks if the path to save ends with a slash and if not, adds it.
@@ -841,19 +843,44 @@ public void ExportDatabase(string pathToSave, string filename, string fileExtens
841843 mutex . WaitOne ( ) ;
842844 }
843845
846+ //Make sure the specified directory exists, else creates it
844847 if ( ! Directory . Exists ( pathToSave ) )
845848 {
846849 Directory . CreateDirectory ( pathToSave ) ;
847850 }
848851
852+ //1. Create temp folder with the database files
853+ string tempFolder = Path . Combine ( pathToSave , Guid . NewGuid ( ) . ToString ( ) . ToUpper ( ) ) ;
854+ if ( ! Directory . Exists ( Path . Combine ( tempFolder ) ) )
855+ {
856+ Directory . CreateDirectory ( tempFolder ) ;
857+ }
858+
859+ //2. Copy database files to the temp folder
860+ foreach ( var database in CurrentDatabases )
861+ {
862+ //Will copy all files matching a database class name
863+ var filesWithDBName = Directory . GetFiles ( CurrentWorkspace , database . Name + ".*" , SearchOption . TopDirectoryOnly ) ;
864+ if ( filesWithDBName . Any ( ) )
865+ {
866+ //Copy files to temp folder
867+ filesWithDBName . ForEach ( f => File . Copy ( f , Path . Combine ( tempFolder , System . IO . Path . GetFileName ( f ) ) , true ) ) ;
868+ }
869+ }
870+
871+ //3. Archive this temp folder to the database file
849872 string fullFilePath = Path . Combine ( pathToSave , filename + fileExtension ) ;
873+ ZipFile . CreateFromDirectory ( tempFolder , fullFilePath ) ;
850874
851- ZipFile . CreateFromDirectory ( CurrentWorkspace , fullFilePath ) ;
875+ //4. Delete temp folder
876+ Directory . Delete ( tempFolder , true ) ;
852877
878+ //Fire events.
853879 OnDatabaseExported ? . Invoke ( this , new OnDatabaseExportedEventArgs ( fullFilePath , DateTime . Now ) ) ;
854880 }
855881 catch
856882 {
883+ //The exception is caught after the mutex is release.
857884 throw ;
858885 }
859886 finally
@@ -866,6 +893,7 @@ public void ExportDatabase(string pathToSave, string filename, string fileExtens
866893 }
867894 catch
868895 {
896+ //Exception is caught here and sent to the caller.
869897 throw ;
870898 }
871899 finally
0 commit comments