private bool LoadFromFile(string filename) { // read in xml data from file TextReader tr = new StreamReader(filename); try { UsersList = new usersList(); XmlSerializer sr = new XmlSerializer(typeof(usersList)); UsersList = (usersList)sr.Deserialize(tr); return(true); } catch (Exception ex) { throw new Exception("UserManager LoadFromFile exception: " + ex.Message); } finally { tr.Close(); } }
void FileChooser_FileOk(object sender, CancelEventArgs e) { FileChooser.Dispose(); if (e.Cancel) { return; } string ImportFileName = FileChooser.FileName; Program.logEvent("Import file name: " + ImportFileName); if (!File.Exists(ImportFileName)) { Program.logEvent("Data file does not exist"); Program.ShowErrorMessage(LanguageTranslation.DATA_FILE_NOT_EXIST, 4000); resetCurrentWorkingDirectory(); return; } #region Import Users if (ImportType == ImportType.User) { try { Program.logEvent("Importing User Data"); if (Path.GetExtension(ImportFileName) == ".xml") { Program.logEvent("XML File identified"); if ((Program.userMgr.ReloadFromFile(ImportFileName))) { Program.logEvent("XML File Verified"); File.Copy(ImportFileName, DestinationFileName, true); Program.logEvent("XML File Copied"); } else { Program.ShowErrorMessage(LanguageTranslation.DATA_FILE_HAS_ERRORS, 4000); Program.logEvent("Error with file during user import"); } } else if (Path.GetExtension(ImportFileName) == ".csv") { Program.logEvent("CSV File identified"); usersList list = new usersList(); //read in list from .csv into List StreamReader reader = new StreamReader(ImportFileName); string currentline; string[] currentlinecomponents; currentline = reader.ReadLine(); //read the first line and ignore it while (!reader.EndOfStream) { currentline = reader.ReadLine(); if (currentline.Trim() != "" && currentline != null) { currentlinecomponents = currentline.Split(','); if (currentlinecomponents.Length == 5) { list.AddUser(new User(currentlinecomponents[0].Trim(), currentlinecomponents[1].Trim(), currentlinecomponents[2].Trim(), currentlinecomponents[3].Trim(), currentlinecomponents[4].Trim())); } else if (currentlinecomponents.Length == 7) { list.AddUser(new User(currentlinecomponents[0].Trim(), currentlinecomponents[1].Trim(), currentlinecomponents[2].Trim(), currentlinecomponents[3].Trim(), currentlinecomponents[4].Trim(), currentlinecomponents[5].Trim(), currentlinecomponents[6].Trim())); } else if (currentlinecomponents.Length == 14) { list.AddUser(new User(currentlinecomponents[0].Trim(), currentlinecomponents[1].Trim(), currentlinecomponents[2].Trim(), currentlinecomponents[3].Trim(), currentlinecomponents[4].Trim(), currentlinecomponents[5].Trim(), currentlinecomponents[6].Trim(), int.Parse(currentlinecomponents[7].Trim()), currentlinecomponents[8].Trim(), currentlinecomponents[9].Trim(), currentlinecomponents[10].Trim(), int.Parse(currentlinecomponents[11].Trim()), currentlinecomponents[12].Trim(), currentlinecomponents[13].Trim())); } } } Program.logEvent("CSV file read into memory"); //serialize list to XML file list.SerializeToXmlFile("temp.xml"); Program.logEvent("CSV file converted to XML"); if ((Program.userMgr.ReloadFromFile("temp.xml"))) { Program.logEvent("XML file Verified"); File.Copy("temp.xml", DestinationFileName, true); Program.logEvent("XML file Copied"); try { File.Delete("temp.xml"); Program.logEvent("XML temp file deleted"); } catch (Exception ex) { Program.logEvent("Error trying to delete temp.xml: " + ex.Message); } } else { Program.ShowErrorMessage(LanguageTranslation.DATA_FILE_HAS_ERRORS, 4000); Program.logEvent("Error with file during user import"); } } } catch (Exception ex) { Program.ShowErrorMessage(LanguageTranslation.DATA_FILE_ERRORS_NOT_EXIST, 4000); Program.logEvent("Error during user import: " + ex.Message); this.Close(); } } #endregion #region Import Access Codes else if (ImportType == ImportType.AccessCode) { try { Program.logEvent("Importing KeyPassword Data"); if (Path.GetExtension(ImportFileName) == ".xml") { Program.logEvent("XML file identified"); if ((Program.passwordMgr.ReloadFromFile(ImportFileName))) { Program.logEvent("XML file verified"); File.Copy(ImportFileName, DestinationFileName, true); Program.logEvent("XML file copied"); } else { Program.ShowErrorMessage(LanguageTranslation.DATA_FILE_HAS_ERRORS, 4000); Program.logEvent("Error with file during department import"); } } else if (Path.GetExtension(ImportFileName) == ".csv") { Program.logEvent("CSV file identified"); KeyPassWordList list = new KeyPassWordList(); //read in list from .csv into List StreamReader reader = new StreamReader(ImportFileName); string currentline; string[] currentlinecomponents; currentline = reader.ReadLine(); //read the first line and ignore it while (!reader.EndOfStream) { currentline = reader.ReadLine(); if ((currentline.Trim() != "") && (currentline != null)) { currentlinecomponents = currentline.Split(','); if (currentlinecomponents.Length == 2) { list.AddKeyPassword(new KeyPassword(currentlinecomponents[0].Trim(), currentlinecomponents[1].Trim())); } else if (currentlinecomponents.Length == 5) { list.AddKeyPassword(new KeyPassword(currentlinecomponents[0].Trim(), currentlinecomponents[1].Trim(), currentlinecomponents[2].Trim(), currentlinecomponents[3].Trim(), currentlinecomponents[4].Trim())); } else if (currentlinecomponents.Length == 9) { list.AddKeyPassword(new KeyPassword(currentlinecomponents[0].Trim(), currentlinecomponents[1].Trim(), currentlinecomponents[2].Trim(), currentlinecomponents[3].Trim(), currentlinecomponents[4].Trim(), currentlinecomponents[5].Trim(), int.Parse(currentlinecomponents[6].Trim()), currentlinecomponents[7].Trim(), currentlinecomponents[9].Trim())); } } } Program.logEvent("CSV file read into memory"); //serialize list to XML file list.SerializeToXmlFile("temp.xml"); Program.logEvent("CSV file converted to XML"); if ((Program.passwordMgr.ReloadFromFile("temp.xml"))) { Program.logEvent("XML file verified"); File.Copy("temp.xml", DestinationFileName, true); Program.logEvent("XML file copied"); try { File.Delete("temp.xml"); Program.logEvent("XML temp file deleted"); } catch (Exception ex) { Program.logEvent("Error trying to delete temp.xml: " + ex.Message); } } else { Program.ShowErrorMessage(LanguageTranslation.DATA_FILE_HAS_ERRORS, 4000); Program.logEvent("Error with file during department import"); } } } catch (Exception ex) { Program.ShowErrorMessage(LanguageTranslation.DATA_FILE_ERRORS_NOT_EXIST, 4000); Program.logEvent("Error during department import: " + ex.Message); this.Close(); } } #endregion button_cancel.Visible = false; SetLabelText(label, LanguageTranslation.IMPORT_COMPLETE); resetCurrentWorkingDirectory(); }