diff --git a/net.nutcore.aliddns/net.nutcore.aliddns/AppConfigHelper.cs b/net.nutcore.aliddns/net.nutcore.aliddns/AppConfigHelper.cs
index 8ece64f..27956a3 100644
--- a/net.nutcore.aliddns/net.nutcore.aliddns/AppConfigHelper.cs
+++ b/net.nutcore.aliddns/net.nutcore.aliddns/AppConfigHelper.cs
@@ -1,4 +1,5 @@
using System;
+using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
@@ -9,20 +10,175 @@ using System.Xml;
namespace net.nutcore.aliddns
{
- public class AppConfigHelper
+ internal class AppConfigHelper
{
- public AppConfigHelper(String configFilePath)
+ System.Configuration.Configuration configFile = null;
+ private static readonly string fileName = "aliddns_config1.xml";
+ ///
+ /// 构造函数
+ ///
+ public AppConfigHelper()
{
- if (!File.Exists(configFilePath))
+ string configFilePath = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, fileName);
+ try
{
-
+ if (!File.Exists(configFilePath))
+ {
+ XmlDocument xmlDoc = new XmlDocument();
+ xmlDoc.AppendChild(xmlDoc.CreateXmlDeclaration("1.0", "utf-8", string.Empty));
+ XmlNode rootNode = xmlDoc.CreateElement("configuration");
+ //rootNode.AppendChild();
+ xmlDoc.AppendChild(rootNode);
+ xmlDoc.Save(configFilePath);
+ // XmlNode rootNode = xmlDoc.CreateElement("appSettings");
+ //XmlTextWriter textWriter = new XmlTextWriter(config_file, null);
+ //textWriter.WriteStartDocument(); //文档开始
+ }
+ ExeConfigurationFileMap map = new ExeConfigurationFileMap();
+ map.ExeConfigFilename = configFilePath;
+ configFile = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
+ }
+ catch(Exception)
+ {
+ Console.WriteLine("Class AppConfigHelper running error!");
}
- System.AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", configFilePath);
}
- public static string GetValueByKey(string key)
+
+ ///
+ /// //添加键值
+ ///
+ ///
+ ///
+ public void AddAppSetting(string key, string value)
+ {
+ configFile.AppSettings.Settings.Add(key, value);
+ configFile.Save();
+ }
+
+ ///
+ /// //修改键值
+ ///
+ ///
+ ///
+ public void SaveAppSetting(string key, string value)
+ {
+ configFile.AppSettings.Settings.Remove(key);
+ configFile.AppSettings.Settings.Add(key, value);
+
+ configFile.Save();
+ }
+
+ ///
+ /// //获得键值
+ ///
+ ///
+ ///
+ public string GetAppSetting(string key)
+ {
+ return configFile.AppSettings.Settings[key].Value;
+ }
+
+ ///
+ /// //移除键值
+ ///
+ ///
+ public void DelAppSetting(string key)
+ {
+ configFile.AppSettings.Settings.Remove(key);
+ configFile.Save();
+ }
+
+ public ArrayList GetXmlElements(string strElem)
+ {
+ ArrayList list = new ArrayList();
+ XmlDocument xmlDoc = new XmlDocument();
+ xmlDoc.Load(System.Windows.Forms.Application.ExecutablePath + ".config");
+ XmlNodeList listNode = xmlDoc.SelectNodes(strElem);
+ foreach (XmlElement el in listNode)
+ {
+ list.Add(el.InnerText);
+ }
+ return list;
+ }
+
+ ///
+ /// 使用当前路径中的指定文件作为配置文件
+ ///
+ /// 配置文件名
+ public void SetConfigFile(string configFileName)
+ {
+ string configFilePath = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, configFileName);
+ System.AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", configFilePath);
+ //ExeConfigurationFileMap map = new ExeConfigurationFileMap();
+ //map.ExeConfigFilename = configFilePath;
+ //configFile = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
+ }
+
+ ///
+ /// 使用指定路径中的指定文件作为配置文件
+ ///
+ /// 配置文件名
+ /// 配置文件路径
+ public void SetConfigFile(string configFileName, string configFileDirectory)
+ {
+ string configFilePath = System.IO.Path.Combine(configFileDirectory, configFileName);
+ System.AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", configFilePath);
+ //ExeConfigurationFileMap map = new ExeConfigurationFileMap();
+ //map.ExeConfigFilename = configFilePath;
+ //configFile = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
+ }
+
+ ///
+ /// 读取配置文件所有内容
+ ///
+ static void ReadAllSettings()
+ {
+ try
+ {
+ var appSettings = ConfigurationManager.AppSettings;
+
+ if (appSettings.Count == 0)
+ {
+ Console.WriteLine("AppSettings is empty.");
+ }
+ else
+ {
+ foreach (var key in appSettings.AllKeys)
+ {
+ Console.WriteLine("Key: {0} Value: {1}", key, appSettings[key]);
+ }
+ }
+ }
+ catch (ConfigurationErrorsException)
+ {
+ Console.WriteLine("Error reading app settings");
+ }
+ }
+
+ ///
+ /// 判断appSettings中是否有此项键名
+ ///
+ /// /// 键名
+ public bool AppSettingsKeyExists(string strKey)
+ {
+ foreach (string key in ConfigurationManager.AppSettings.AllKeys)
+ {
+ if (key == strKey)
+ {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ ///
+ /// 获取设置文件中某个键的值
+ ///
+ /// 键名
+ public static string GetValueByKey(string strKey)
{
ConfigurationManager.RefreshSection("appSettings");
- return ConfigurationManager.AppSettings[key];
+ return ConfigurationManager.AppSettings[strKey];
}
public static void ModifyAppSettings(string strKey, string value)
{
diff --git a/net.nutcore.aliddns/net.nutcore.aliddns/Form_main.cs b/net.nutcore.aliddns/net.nutcore.aliddns/Form_main.cs
index b17eaf7..ea96934 100644
--- a/net.nutcore.aliddns/net.nutcore.aliddns/Form_main.cs
+++ b/net.nutcore.aliddns/net.nutcore.aliddns/Form_main.cs
@@ -21,11 +21,8 @@ namespace net.nutcore.aliddns
static DefaultAcsClient client;
//初始化ngrok操作类
private Ngrok ngrok = new Ngrok();
- //指定配置文件为aliddns_config.config
- static String configFilePath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "aliddns_config.cfg");
- //获得程序默认配置文件*.exe.config(或*.vhost.exe.config)的全路径
- //static String configFilePath = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
- private AppConfigHelper cfg = new AppConfigHelper(configFilePath);
+ private AppConfigHelper cfg = new AppConfigHelper();
+
public mainForm()
{
InitializeComponent();
@@ -854,8 +851,8 @@ namespace net.nutcore.aliddns
}
else
return null;
- httpClient.Dispose();
- response.Dispose();
+ //httpClient.Dispose();
+ //response.Dispose();
}
catch (Exception error)
{