/// <summary> /// Deletes an Item from the Configuration /// </summary> /// <param name="connectionName">Name of the Connection</param> /// <param name="wipeData">if this is set, all data within the local folder will be wiped out!</param> public static void Delete(string connectionName, bool wipeData = false) { if (File.Exists(StoragePath)) { string json = File.ReadAllText(StoragePath); List <Xrm.CrmConnection> crmConnections = JsonConvert.DeserializeObject <List <Xrm.CrmConnection> >(json); try { Xrm.CrmConnection crmConnection = crmConnections.SingleOrDefault(x => x.Name == connectionName); crmConnections.Remove(crmConnection); if (wipeData) { Directory.Delete(crmConnection.LocalPath, true); } if (crmConnections.Count == 0) { // Delete File if there is no Connection left File.Delete(StoragePath); } else { string jsonNew = JsonConvert.SerializeObject(crmConnections); File.WriteAllText(StoragePath, jsonNew); } } catch (Exception ex) { Log.Error(ex.Message, ex); throw; } } }
/// <summary> /// Updates an existing CRM Connection /// </summary> /// <param name="crmConnection">CRM Connection <see cref="Xrm.CrmConnection"/></param> /// <param name="encryptionKey">The Encryption key used in <see cref="Core.Data.Cryptography"/></param> public static void Update(Xrm.CrmConnection crmConnection, string encryptionKey) { string json = File.ReadAllText(StoragePath); List <Xrm.CrmConnection> crmConnections = JsonConvert.DeserializeObject <List <Xrm.CrmConnection> >(json); crmConnections.Find(x => x.ConnectionID == crmConnection.ConnectionID).ConnectionString = Cryptography.EncryptStringAES(crmConnection.ConnectionString, encryptionKey); crmConnections.Find(x => x.ConnectionID == crmConnection.ConnectionID).LocalPath = crmConnection.LocalPath; crmConnections.Find(x => x.ConnectionID == crmConnection.ConnectionID).Name = crmConnection.Name; string jsonNew = JsonConvert.SerializeObject(crmConnections); File.WriteAllText(StoragePath, jsonNew); }
/// <summary> /// Saves the <see cref="Xrm.CrmConnection"/> to the local Configuration /// </summary> /// <param name="crmConnection">CRM Connection <see cref="Xrm.CrmConnection"/></param> /// <param name="encryptionKey">The Encryption key used in <see cref="Core.Data.Cryptography"/></param> public static void Save(Xrm.CrmConnection crmConnection, string encryptionKey) { List <Xrm.CrmConnection> crmConnections = new List <Xrm.CrmConnection>(); // Create if File does not exist if (!File.Exists(StoragePath)) { File.Create(StoragePath).Close(); // Encrypt Connection String crmConnection.ConnectionString = Cryptography.EncryptStringAES(crmConnection.ConnectionString, encryptionKey); crmConnection.LocalPath = string.Empty; crmConnections.Add(crmConnection); string json = JsonConvert.SerializeObject(crmConnections); File.WriteAllText(StoragePath, json); } else { using (StreamReader streamReader = new StreamReader(Data.StorageExtensions.StoragePath)) { string localJSON = streamReader.ReadToEnd(); // Convert Json to List crmConnections = JsonConvert.DeserializeObject <List <Xrm.CrmConnection> >(localJSON); // Encrypt Connection String crmConnection.ConnectionString = Cryptography.EncryptStringAES(crmConnection.ConnectionString, encryptionKey); crmConnection.LocalPath = string.Empty; crmConnections.Add(crmConnection); // Close File Stream streamReader.Close(); } // Write to Configuration File string json = JsonConvert.SerializeObject(crmConnections); File.WriteAllText(StoragePath, json); } }