利用系統加密 App Configure

        public static void EncryptConfigSection(string sectionName)
        {

            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            ConfigurationSection section = config.GetSection(sectionName);

            if (section != null)
            {

                if (!section.IsReadOnly())
                {

                    section.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");

                    section.SectionInformation.ForceSave = true;

                    config.Save(ConfigurationSaveMode.Full);

                }

            }

            ConfigurationManager.RefreshSection(sectionName);

        }

 

更新 Configuration 設定

public static void UpdateCnn(string strName,string strValue)
        {

            ExeConfigurationFileMap map = new ExeConfigurationFileMap();
            map.ExeConfigFilename = Application.ExecutablePath + ".config";

            //Configuration config = ConfigurationManager.OpenExeConfiguration(fi.FullName);
            Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
            if (config.ConnectionStrings != null )
            {
                if (config.ConnectionStrings.ConnectionStrings[strName] != null)
                {
                    config.ConnectionStrings.ConnectionStrings[strName].ConnectionString = strValue;
                    config.Save(ConfigurationSaveMode.Modified);
                    ConfigurationManager.RefreshSection("connectionStrings");
                }
            }
       
        }

http://cid-bf14192bd27975cb.skydrive.live.com/self.aspx/Source/NET/EncryptAndUpdateAppConfiguration.zip

發表迴響

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 變更 )

Twitter picture

You are commenting using your Twitter account. Log Out / 變更 )

Facebook照片

You are commenting using your Facebook account. Log Out / 變更 )

Google+ photo

You are commenting using your Google+ account. Log Out / 變更 )

連結到 %s

%d 位部落客按了讚: