/// <summary> /// Save sessions to the sessions.xml configuration file. /// </summary> public void SaveSessionsData(List <Dictionary <string, object> > sessions_data) { try { String filePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "PuTTYStorm", "sessions.xml"); if (!Directory.Exists(Path.Combine(Environment.GetFolderPath (Environment.SpecialFolder.MyDocuments), "PuTTYStorm"))) { Directory.CreateDirectory(Path.Combine(Environment.GetFolderPath (Environment.SpecialFolder.MyDocuments), "PuTTYStorm")); } using (XmlWriter writer = XmlWriter.Create(filePath)) { writer.WriteStartDocument(); writer.WriteStartElement("Sessions"); foreach (Dictionary <string, object> dictionary in sessions_data) { if (dictionary["hostname"].ToString() != "" && dictionary["username"].ToString() != "") { writer.WriteStartElement("Session"); writer.WriteElementString("hostname", dictionary["hostname"].ToString()); writer.WriteElementString("username", dictionary["username"].ToString()); writer.WriteElementString("password", AESEncryptDecrypt.Encrypt(dictionary["password"].ToString())); writer.WriteElementString("count", dictionary["c_count"].ToString()); writer.WriteElementString("group", dictionary["group"].ToString()); writer.WriteElementString("subgroup", dictionary["sub_group"].ToString()); writer.WriteEndElement(); } } writer.WriteEndElement(); writer.WriteEndDocument(); writer.Close(); if (writer.WriteState == WriteState.Closed) { GlobalVar.ConfigSavedSuccessfully = true; } } } catch (Exception e) { GlobalVar.ConfigSavedSuccessfully = false; MessageBox.Show("Failed to save sessions! Try again! " + e.Message.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop); } Backup_Sessions(); }
/// <summary> /// Save private keys to the privatekeys.xml configuration file. /// </summary> public void Save_PrivateKeys(List <Panel> PrivateKeys) { String filePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "PuTTYStorm", "privatekeys.xml"); if (!Directory.Exists(Path.Combine(Environment.GetFolderPath (Environment.SpecialFolder.MyDocuments), "PuTTYStorm"))) { Directory.CreateDirectory(Path.Combine(Environment.GetFolderPath (Environment.SpecialFolder.MyDocuments), "PuTTYStorm")); } using (XmlWriter writer = XmlWriter.Create(filePath)) { writer.WriteStartDocument(); writer.WriteStartElement("PrivateKeys"); foreach (Panel container in PrivateKeys) { string pk_name = null; string pk_type = null; string pk_group = null; string pk_pwd = null; foreach (Control control in container.Controls) { if (control.Name == "pk_name_label") { pk_name = control.Text; } if (control.Name == "pk_type_label") { pk_type = control.Text.Replace("Type: ", string.Empty); } if (control.Name == "pk_group_label") { pk_group = control.Text.Replace("Group: ", string.Empty); } if (control.Name == "private_keys_hidden_passphrase_textbox") { pk_pwd = control.Text; } } if (pk_name != "") { writer.WriteStartElement("PrivateKey"); writer.WriteElementString("name", pk_name); writer.WriteElementString("type", pk_type); writer.WriteElementString("group", pk_group); if (pk_pwd == "" || pk_pwd == null) { writer.WriteElementString("pwd", " "); } else { writer.WriteElementString("pwd", AESEncryptDecrypt.Encrypt(pk_pwd)); } writer.WriteEndElement(); } } writer.WriteEndElement(); writer.WriteEndDocument(); } GetSavedSessions saved_data = new GetSavedSessions(); SavedPrivatekeysInfo privatekeys = null; if (File.Exists(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "PuTTYStorm", "privatekeys.xml"))) { privatekeys = saved_data.get_PrivateKeys(); } if (privatekeys.names.Count != 0) { Backup_PrivateKeys(); } }