namespace HW.Utility { using System; using System.Configuration; public static class ProtectAppConfig { public static T OpenConfigFile(string pathName, string stringSection) where T: ConfigurationSection { System.Configuration.Configuration configuration = null; T section = default(T); try { ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap(); fileMap.ExeConfigFilename = pathName; configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None); if (configuration != null) { section = configuration.GetSection(stringSection) as T; } } catch (Exception exception) { throw exception; } return section; } public static void ProtectConfigFiles(string pathName, string protectProvider, string protectSection) { if (protectSection == "All") { ToggleConnectionStringProtection(pathName, true, protectProvider); ToggleAppSettingProtection(pathName, true, protectProvider); } else if (protectSection == "connectionStrings") { ToggleConnectionStringProtection(pathName, true, protectProvider); } else { ToggleAppSettingProtection(pathName, true, protectProvider); } } private static void ToggleAppSettingProtection(string pathName, bool protect, string protectProvider) { System.Configuration.Configuration configuration = null; AppSettingsSection section = null; bool flag = false; try { ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap(); fileMap.ExeConfigFilename = pathName; configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None); if (configuration != null) { section = configuration.GetSection("appSettings") as AppSettingsSection; if (section != null) { if (!section.ElementInformation.IsLocked && !section.SectionInformation.IsLocked) { if (protect) { if (!section.SectionInformation.IsProtected) { flag = true; section.SectionInformation.ProtectSection(protectProvider); } } else if (section.SectionInformation.IsProtected) { flag = true; section.SectionInformation.UnprotectSection(); } } if (flag) { section.SectionInformation.ForceSave = true; configuration.Save(ConfigurationSaveMode.Modified); } } } } catch (Exception exception) { throw exception; } } private static void ToggleConnectionStringProtection(string pathName, bool protect, string protectProvider) { System.Configuration.Configuration configuration = null; ConnectionStringsSection section = null; bool flag = false; try { ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap(); fileMap.ExeConfigFilename = pathName; configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None); if (configuration != null) { section = configuration.GetSection("connectionStrings") as ConnectionStringsSection; if (section != null) { if (!section.ElementInformation.IsLocked && !section.SectionInformation.IsLocked) { if (protect) { if (!section.SectionInformation.IsProtected) { flag = true; section.SectionInformation.ProtectSection(protectProvider); } } else if (section.SectionInformation.IsProtected) { flag = true; section.SectionInformation.UnprotectSection(); } } if (flag) { section.SectionInformation.ForceSave = true; configuration.Save(ConfigurationSaveMode.Modified); } } } } catch (Exception exception) { throw exception; } } public static void UnProtectConfigFiles(string pathName, string protectProvider, string protectSection) { if (protectSection == "All") { ToggleConnectionStringProtection(pathName, false, protectProvider); ToggleAppSettingProtection(pathName, false, protectProvider); } else if (protectSection == "connectionStrings") { ToggleConnectionStringProtection(pathName, false, protectProvider); } else { ToggleAppSettingProtection(pathName, false, protectProvider); } } public static bool UpdateConnectionString(string pathName, string stringSection, string strContent) { System.Configuration.Configuration configuration = null; ConnectionStringsSection section = null; AppSettingsSection section2 = null; bool flag = false; try { ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap(); fileMap.ExeConfigFilename = pathName; if (fileMap != null) { configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None); } if (configuration == null) { return flag; } if (stringSection == "connectionStrings") { section = configuration.GetSection(stringSection) as ConnectionStringsSection; if ((section != null) && !(section.ElementInformation.IsLocked || section.SectionInformation.IsLocked)) { section.ConnectionStrings["FBMSConnection"].ConnectionString = strContent; section.SectionInformation.ForceSave = true; configuration.Save(ConfigurationSaveMode.Modified); flag = true; } return flag; } if (!(stringSection == "appSettings")) { return flag; } section2 = configuration.GetSection(stringSection) as AppSettingsSection; if (section2 == null) { return flag; } if (!(section2.ElementInformation.IsLocked || section2.SectionInformation.IsLocked)) { section2.Settings["VerifyURL"].Value = strContent; section2.SectionInformation.ForceSave = true; configuration.Save(ConfigurationSaveMode.Modified); flag = true; } } catch (Exception exception) { throw exception; } return flag; } } }