private void btnTransfer_Click(object sender, EventArgs e) { // TODO: use a cached transfered account for cases of failure // TODO: decide if source profile should be auto saved or not string profilePath = Path.Combine(Globals.Info.profilesPath, cbDest.Items[cbDest.SelectedIndex].ToString()) + ".json"; Class.ProfileInfo _destProfile = Utils.Account.Load(profilePath); if (_destProfile == null) { MessageBox.Show("Failed to load destination profile!", "Transfer account", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } foreach (KeyValuePair <string, Class.Account> _account in Utils.ProfileInfo.GetSelectedItems()) { _destProfile.Profiles[_account.Key] = _account.Value; if (cbRemove.Checked) { // Remove the account first before removing the lvi since other threads might access a dead lvi from a disposing account instance ref ListViewItem _lvi_tmp = ref _account.Value.LVI; Globals.CurrentProfile.Profiles.Remove(_account.Key); _lvi_tmp.Remove(); } }
public static bool Save(ref Class.ProfileInfo _profileInfo, string _profilePath) { try { _profileInfo.LastSaved = DateTime.Now; // Update last saved info _profileInfo.vProfileFormat = Globals.Info.vProfileFormat; // Update the saved profile JSON format version using (StreamWriter _sw = new StreamWriter(_profilePath, false, System.Text.Encoding.UTF8)) { _sw.WriteLine(JsonConvert.SerializeObject(_profileInfo)); _sw.Close(); } return(true); } catch (Exception ex) { MessageBox.Show($"Profile: {_profilePath}\nException: {ex}", "Failed to save profile", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } }