private static void GetLanguage() { string retrievedLanguage; // TODO important: Search for a method that checks for null value - conditional operator try { FileLocking ConfugurationFile_Locking = new FileLocking(); XDocument Xdoc = null; Exception error = null; Xdoc = XDocument.Load(ConfugurationFile_Locking.ReadFile(applicationConfigurationFile, ref error)); retrievedLanguage = Xdoc.Element("ConfigFile").Element("ApplicationSettings").Element("GeneralSettings").Element("Language").Value.ToString(); } catch (Exception _error) { // TODO: Write imediately to the XML file and set a protocol entry that there was no language MessageBox.Show(_error.Message + " - " + applicationConfigurationFile); retrievedLanguage = "English"; } switch (retrievedLanguage) { case "English": { CurrentLanguagePublic = Languages.English; } break; case "German": { CurrentLanguagePublic = Languages.German; } break; default: { CurrentLanguagePublic = Languages.English; // TODO: // Create an entry in the protocol file that the language could not be retrieved } break; } }
private void DependenciesCheck() { SQLiteConnection.CreateFile("smDB.sqlite"); SQLiteConnection m_dbConnection = new SQLiteConnection("Data Source=smDB.sqlite;Version=3;"); m_dbConnection.Open(); string sql = "create table highscores (name varchar(20), score int)"; SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection); command.ExecuteNonQuery(); sql = "insert into highscores (name, score) values ('Me', 9001)"; command = new SQLiteCommand(sql, m_dbConnection); command.ExecuteNonQuery(); m_dbConnection.Close(); // Check if libraries are available //if (!File.Exists(CoreFunctions.GetExecutingDirectoryName() + "StockManagementCore.dll")) //{ // MessageBox.Show("Die erforderliche Datei \"StockManagementCore.dll\" wurde nicht gefunden, das Programm wird nun beendet.", "Fehler beim Laden", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); // this.Close(); //} // Check if the xlsx library exists //if (!File.Exists(CoreFunctions.GetExecutingDirectoryName() + "DocumentFormat.OpenXml.dll")) //{ // MessageBox.Show("Die erforderliche Datei \"DocumentFormat.OpenXml.dll\" wurde nicht gefunden, das Programm wird nun beendet", "Fehler beim Laden", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); // this.Close(); //} // Check if the english localisation library is available //if (!File.Exists(CoreFunctions.GetExecutingDirectoryName() + "Localisation.dll")) //{ // MessageBox.Show("Die erforderliche Datei \"Localisation.dll\" wurde nicht gefunden, das Programm wird nun beendet", "Fehler beim Laden", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); // this.Close(); //} // Check if the german localisation folder and library is available //if (!Directory.Exists(CoreFunctions.GetExecutingDirectoryName() + "de")) //{ // Directory.CreateDirectory(CoreFunctions.GetExecutingDirectoryName() + "de"); // MessageBox.Show("The folder for the german localisation does not exist, the folder will be created now but the localisation file is missing." + Environment.NewLine + "Do you want to download it now?", "Fehler beim Laden", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); //} //else //{ // if (!File.Exists(CoreFunctions.GetExecutingDirectoryName() + "de\\Localisation.resources.dll")) // { // MessageBox.Show("Die erforderliche Datei \"Localisation.resources..dll\" wurde nicht gefunden, das Programm wird nun beendet", "Fehler beim Laden", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); // } //} #region Configuration file locking and checking confugurationFileLocking = new FileLocking(); // Try to lock the configuration file if (File.Exists(applicationConfigurationFile)) { if (confugurationFileLocking.OpenAndLock(applicationConfigurationFile, FileAccess.ReadWrite, FileShare.Read, ref applicationWideException)) { CreateBackup(); // If there is no root element asign null to Xdoc try { xdoc = XDocument.Load(confugurationFileLocking.LockedFile); } catch (Exception _error) { xdoc = null; // TODO: Protocol it! } // If Xdoc equals null or not equals "ConfigFile" the whole document is about to be recreated if (xdoc != null) { if (xdoc.Root.Name == "ConfigFile") { ReadXMLConfiguration(true); } else { ReadXMLConfiguration(false); } } else { ReadXMLConfiguration(false); } // Set the current user of the program and save it to the configuration file xdoc.Element("ConfigFile").Element("ApplicationSettings").Element("GeneralSettings").Element("CurrentUser").Value = Environment.UserName; confugurationFileLocking.WriteXDocument(xdoc); } else { xdoc = XDocument.Load(confugurationFileLocking.ReadFile(applicationConfigurationFile, ref applicationWideException)); currentUser = xdoc.Element("ConfigFile").Element("ApplicationSettings").Element("GeneralSettings").Element("CurrentUser").Value.ToString(); isConfigurationFileLocked = true; TrackNotificationFile(); } doesConfigurationFileExists = true; } else { // Set the language to english because the configuration file is missing and english is the default language Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("en-US"); } #endregion }
private void SettingsMenu_FormClosing(object sender, FormClosingEventArgs e) { if (File.Exists(DataFileText.Text)) { if (File.Exists(applicationConfigurationFile)) { if (MainMenuPublic != null) { MainMenuPublic.Xdoc.Element("ConfigFile").Element("ApplicationSettings").Element("DataReferences").Element("CustomerFile").Value = DataFileText.Text; ConfugurationFileLocking.WriteXDocument(MainMenuPublic.Xdoc); } else { StockManagementMenu.Xdoc.Element("ConfigFile").Element("ApplicationSettings").Element("DataReferences").Element("CustomerFile").Value = DataFileText.Text; ConfugurationFileLocking.WriteXDocument(StockManagementMenu.Xdoc); } // TODO: clear } else { ConfugurationFileLocking = new FileLocking(); if (ConfugurationFileLocking.CreateAndLock(applicationConfigurationFile, FileAccess.ReadWrite, FileShare.Read, ref error)) { using (XmlWriter writer = XmlWriter.Create(ConfugurationFileLocking.LockedFile)) { writer.WriteStartDocument(); writer.WriteComment("This file is generated by the program. It contains application settings and all customer data."); writer.WriteStartElement("ConfigFile"); writer.WriteStartElement("ApplicationSettings"); // DataReferences element writer.WriteStartElement("DataReferences"); writer.WriteElementString("CustomerFile", DataFileText.Text); writer.WriteEndElement(); // DataReferences element writer.WriteStartElement("GeneralSettings"); writer.WriteElementString("CurrentUser", Environment.UserName); switch (LanguageHolder.Text) { case "English": case "Englisch": { writer.WriteElementString("Language", "English"); } break; case "German": case "Deutsch": { writer.WriteElementString("Language", "German"); } break; default: { // Placeholder - will never be reached // TODO: Create a protocol just to catch that case } break; } writer.WriteEndElement(); writer.WriteEndElement(); writer.WriteStartElement("CustomerData"); writer.WriteEndElement(); writer.WriteEndDocument(); } // Asign the just created filestream to the variable in the main menu, do the same with the XDocument MainMenuPublic.ConfugurationFileLocking = ConfugurationFileLocking; MainMenuPublic.Xdoc = XDocument.Load(ConfugurationFileLocking.LockedFile); } } if (!this.Owner.Visible) { this.Owner.Visible = true; } this.Owner.BringToFront(); } else { MessageBox.Show(Localisation.Localisation.strings.SettingsMenu_ValidFile); e.Cancel = true; } }