码迷,mamicode.com
首页 > Windows程序 > 详细

C# 如何写入exe.config文件

时间:2016-07-06 09:59:56      阅读:253      评论:0      收藏:0      [点我收藏+]

标签:

public static string GetConnectionStringsConfig(string connectionName)
        {
            string file = System.Windows.Forms.Application.ExecutablePath;
            System.Configuration.Configuration sysconfig = ConfigurationManager.OpenExeConfiguration(file);
            string connectionString =
                sysconfig.ConnectionStrings.ConnectionStrings[connectionName].ConnectionString.ToString();
            return connectionString;
        }
        public static void UpdateConnectionStringsConfig(string newName, string newConString)
        {
            string file = System.Windows.Forms.Application.ExecutablePath;
            Configuration sysconfig = ConfigurationManager.OpenExeConfiguration(file);
            bool exist = false;
            if (sysconfig.ConnectionStrings.ConnectionStrings[newName] != null)
            {
                exist = true;
            }
            if (exist)
            {
                sysconfig.ConnectionStrings.ConnectionStrings.Remove(newName);
            }
            ConnectionStringSettings mySettings =
                new ConnectionStringSettings(newName, newConString);
            sysconfig.ConnectionStrings.ConnectionStrings.Add(mySettings);
            sysconfig.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("ConnectionStrings");
        }

 

C# 如何写入exe.config文件

标签:

原文地址:http://www.cnblogs.com/duan425/p/5645774.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!