예제 #1
0
 /// <summary>
 /// Closes the currently opened database.
 /// </summary>
 /// <param name="communicator">The <see cref="AccessComm"/> which holds the opened database.</param>
 private void CloseDB(ref AccessComm communicator)
 {
     if (communicator?.IsDisposed == false)
     {
         communicator.Dispose();
         Console.WriteLine("Closed database.");
     }
     else
     {
         Console.WriteLine("There is no database to close.");
     }
 }
예제 #2
0
 /// <summary>
 /// Closes the log file and database and saves the current language to a config file.
 /// </summary>
 private void App_OnExit(object sender, ExitEventArgs e)
 {
     SaveLanguageToFile(Languages.Resources.Culture);
     _accessDB.Dispose();
     Log.Close();
 }
예제 #3
0
        /// <summary>
        /// Tries to open a database by asking the user for a path and password.
        /// </summary>
        /// <param name="communicator">The <see cref="AccessComm"/> to actually open the database.</param>
        private void OpenDB(ref AccessComm communicator)
        {
            //Checks if a database is already open
            if (communicator?.IsDisposed == false)
            {
                Console.WriteLine("A database has already been opened!");
                return;
            }

            //Asks the user for a db path
            Console.Write("Enter the path to a database (MS Access 2007-2016): ");
            string dbPath = Console.ReadLine();

            //Asks the user for the db password, saving it in a secure string
            Console.Write("Enter the database password (if available): ");
            var dbPass = new SecureString();

            while (true)
            {
                //Reads the next key but doesn't display it in console
                ConsoleKeyInfo keystroke = Console.ReadKey(true);

                //Checks if the user pressed enter to finish writing the password
                if (keystroke.KeyChar == (char)ConsoleKey.Enter)
                {
                    break;
                }

                //Checks if the user wants to delete a character
                if (keystroke.KeyChar == (char)ConsoleKey.Backspace)
                {
                    //Skips deleting íf character length is already 0
                    if (dbPass.Length <= 0)
                    {
                        continue;
                    }

                    //Removes the last character
                    dbPass.RemoveAt(dbPass.Length - 1);

                    //Removes the last character from console
                    Console.CursorLeft--;
                    Console.Write(" ");
                    Console.CursorLeft--;
                }
                else
                {
                    //Writes the character both to the secure string and the console (censored)
                    dbPass.AppendChar(keystroke.KeyChar);
                    Console.Write("*");
                }
            }
            Console.WriteLine();

            //Tries to open the database with the specified data
            try
            {
                communicator = new AccessComm(dbPath, dbPass);
            }
            catch (Exception e)
            {
                Console.WriteLine($"Could not open database! Exception: {e.Message}");

                if (communicator?.IsDisposed == false)
                {
                    communicator.Dispose();
                }

                return;
            }
            Console.WriteLine("Opened database.");
        }