给Config的appSettings节点赋值

 /// <summary>
        /// 更新config的AppSettings配置值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="newValue"></param>
        /// <returns></returns>
        private bool UpdateAppConfig(string key, string newValue)
        {
            bool isModified = false;
            foreach (string appkey in ConfigurationManager.AppSettings)
            {
                if (key == appkey)
                {
                    isModified = true;
                }
            }       // Open App.Config of executable                
            // You need to remove the old settings object before you can replace it      
            if (isModified)
            {
                Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                config.AppSettings.Settings[key].Value = newValue;
                config.Save(ConfigurationSaveMode.Modified);
                ConfigurationManager.RefreshSection("appSettings");
                return true;
                //config.AppSettings.Settings.Remove(newKey);
            }
            else
            {
                return false;
            }
        }

 

 /// <summary>
        /// 计算文件的hash值 
        /// </summary>
        public static string CalcFileHash(string FilePath)
        {
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            byte[] hash;
            using (FileStream fs = new FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096))
            {
                hash = md5.ComputeHash(fs);
            }
            return BitConverter.ToString(hash);
        }

 

posted @ 2017-04-25 11:03  努力的小样  阅读(696)  评论(0编辑  收藏  举报
Copyright ©2017 爱睡觉的程序猿