C#Winform中保存配置信息App.Config的类操作日志操作

C#Winform中保存配置信息App.Config的类操作:

添加引用:System.Configuration;

/// <summary>
    /// 配置信息维护
    /// </summary>
    public class AppConfig
    {
        public static Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

        /// <summary>
        /// 获取配置值
        /// </summary>
        /// <param name="key">配置标识</param>
        /// <returns></returns>
        public static string GetValue(string key)
        {
            string result = string.Empty;
            if (config.AppSettings.Settings[key] != null)
                result = config.AppSettings.Settings[key].Value;
            return result;
        }

        /// <summary>
        /// 修改或增加配置值
        /// </summary>
        /// <param name="key">配置标识</param>
        /// <param name="value">配置值</param>
        public static void SetValue(string key, string value)
        {
            if (config.AppSettings.Settings[key] != null)
                config.AppSettings.Settings[key].Value = value;
            else
                config.AppSettings.Settings.Add(key, value);
            config.Save(ConfigurationSaveMode.Modified);
        }

        /// <summary>
        /// 删除配置值
        /// </summary>
        /// <param name="key">配置标识</param>
        public static void DeleteValue(string key)
        {
            config.AppSettings.Settings.Remove(key);
        }
    }

日志操作:

/// <summary>
    /// 日志操作
    /// </summary>
    public class LogAccess
    {
        /// <summary>
        /// 写入日志
        /// </summary>
        /// <param name="logContent"></param>
        public static void WriteLog(string logContent)
        {
            try
            {
                string logName = DateTime.Now.ToString("yyyy-MM-dd") + ".log";
                FileStream fileStream = new FileStream(logName, FileMode.Append);
                StreamWriter writer = new StreamWriter(fileStream);
                writer.WriteLine(string.Format("{0}错误:{1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ffffff"), logContent));
                writer.Close();
                fileStream.Close();
            }
            catch 
            {
            }
        }
    }



你可能感兴趣的:(C#Winform中保存配置信息App.Config的类操作日志操作)