private void buttonCreateDatabase_Click(object sender, EventArgs e) { char[] CharacterArrayOfDatabaseName = textBoxCreateName.Text.ToCharArray(); // Create an array of characters from the provided string CharacterArrayOfDatabaseName = Array.FindAll <char>(CharacterArrayOfDatabaseName, (c => (char.IsLetterOrDigit(c) || // Loop through the array looking for anything but letters, white space or a dash char.IsWhiteSpace(c) || c == '-'))); textBoxCreateName.Text = new string(CharacterArrayOfDatabaseName); // Output the character array as a new string Boolean canCreate = true; // Create a variable to allow creation of the database string[] Databases = Directory.GetFiles(@"./databases/", textBoxCreateName.Text + ".sqlite"); // Create an array of all files ending in .sqlite within the databases folder foreach (string DataBaseName in Databases) // For every result { canCreate = false; // If a database exists with that name, disalow its creation } if (canCreate == false) // If the database can't be created, explain why. { labelErrorCreation.Visible = true; // Show error that the database already exists labelErrorCreation.ForeColor = Color.Red; labelErrorCreation.Text = "Error: A database with that name\r\nalready exists."; } else { SQLiteConnection.CreateFile("./databases/" + textBoxCreateName.Text + ".sqlite"); // Use the SQLiteConnection to create the sqlite file SQLiteConnection m_dbConnection = new SQLiteConnection("Data Source=./databases/" + textBoxCreateName.Text + ".sqlite;Version=3;"); m_dbConnection.Open(); string sql = "create table reminders (name varchar(255), date varchar(255), time varchar(255))"; SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection); command.ExecuteNonQuery(); m_dbConnection.Close(); labelErrorCreation.ForeColor = Color.Green; // Show success message labelErrorCreation.Visible = true; labelErrorCreation.Text = "successfully created database: " + textBoxCreateName.Text; if (textBoxCreatePassword.Text != "") { Helpers.AESEncrypt("./databases/" + textBoxCreateName.Text + ".sqlite", textBoxCreatePassword.Text); labelErrorCreation.Text = "successfully created and\r\nencrypted database: " + textBoxCreateName.Text; File.Delete("./databases/" + textBoxCreateName.Text + ".sqlite"); } lookForDatabases(); } }
static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); // Ensure there is a folder for local SQL databases System.IO.Directory.CreateDirectory("./databases"); // Create required folder //Changed to show the CreateOrLoadDB form rather than the MainWindow form var main_form = new Windows.CreateOrLoadDB(); main_form.Show(); Application.Run(); // Run code here to encrypt database if (Globals.DatabaseEncryption) { Helpers.AESEncrypt("./databases/" + Globals.DatabaseName + ".sqlite.aes.decrypted", Globals.DatabasePassword); File.Delete("./databases/" + Globals.DatabaseName + ".sqlite.aes"); File.Delete("./databases/" + Globals.DatabaseName + ".sqlite.aes.decrypted"); File.Move("./databases/" + Globals.DatabaseName + ".sqlite.aes.decrypted.aes", "./databases/" + Globals.DatabaseName + ".sqlite.aes"); } }