zoukankan      html  css  js  c++  java
  • 操作config文件

    Configuration objConfig = ConfigurationManager.OpenExeConfiguration(System.Windows.Forms.Application.StartupPath + @"\TestConfig.exe");

    //需要有TestConfig.exe文件
                    ConnectionStringsSection objAppsettings = null;
                    objAppsettings = (ConnectionStringsSection)objConfig.GetSection("connectionStrings");
                    foreach (ConnectionStringSettings s in objAppsettings.ConnectionStrings)
                    {
                        Console.WriteLine(s.Name);
                        if (s.Name == "ConnectionString")
                        {
                            string ss;
                            if (s.LockItem == false)
                            {
                                ss = s.ConnectionString;
                                s.ConnectionString += " encrypt";
                                s.LockItem = true;
                                objConfig.Save(ConfigurationSaveMode.Modified);
                            }
                            else
                            {
                                ss = s.ConnectionString;

                            }
                            Console.WriteLine(ss);
                        }
                    }
                    Console.ReadKey();

                    if (!objAppsettings.SectionInformation.IsProtected)
                    {
                        objAppsettings.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");
                        objAppsettings.SectionInformation.ForceSave = true;
                        objConfig.Save(ConfigurationSaveMode.Modified);
                    }
                    else
                    {
                        objAppsettings.SectionInformation.UnprotectSection();
                        objAppsettings.SectionInformation.ForceSave = true;
                        objConfig.Save(ConfigurationSaveMode.Modified);
                    }

  • 相关阅读:
    编写高质量代码改善C#程序的157个建议——建议7: 将0值作为枚举的默认值
    编写高质量代码改善C#程序的157个建议——建议6: 区别readonly和const的使用方法
    编写高质量代码改善C#程序的157个建议——建议5: 使用int?来确保值类型也可以为null
    编写高质量代码改善C#程序的157个建议——建议4: TryParse比Parse好
    基于cookie实现用户验证
    页面分页自定义插件
    DOM详习讲解
    HTML标签详细讲解
    Tornado模板配置
    biginteger转Long
  • 原文地址:https://www.cnblogs.com/djian/p/1922698.html
Copyright © 2011-2022 走看看