none
使用ConfigurationManager更新指定config文件产生一个新的config文件疑问? RRS feed

  • 问题

  • 执行更新config文件后,那个config文件名被修改了。例如,本来原来的文件名是“Script.config”,执行下面代码后,重新生成了一个文件“Script.config.config”,而这个新生成的文件才包含下面这段代码的执行。


          

    Code Snippet

      private void UpdateConfigFile()
            {
                System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(@"C:\Script.config");

                int appStgCnt = ConfigurationManager.AppSettings.Count;
                string newKey = "NewKey" + appStgCnt.ToString();
                string newValue = DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString();

                config.AppSettings.Settings.Add(newKey, newValue);

                config.Save(ConfigurationSaveMode.Modified);
                ConfigurationManager.RefreshSection("appSettings");


                ConnectionStringSettings c = new ConnectionStringSettings();
                c.ConnectionString = @"ConnectionString";
                c.ProviderName = "ProviderName";
                c.Name = @"Name";

                config.ConnectionStrings.ConnectionStrings.Add(c);

                config.Save(ConfigurationSaveMode.Modified);
                ConfigurationManager.RefreshSection("connectionStrings");
            }

     

     

     

    thanks。

    2008年7月28日 7:47

答案

  • 是你的OpenExeConfiguration用的不对。

    我帮你改了一下,你再去试一试。

     

     

        

    Code Snippet

        private void UpdateConfigFile()
            {
                string exeFilePath = System.IO.Path.Combine(
           Environment.CurrentDirectory, @"Script.config");

                // Map to the application configuration file.
                ExeConfigurationFileMap configFile = new ExeConfigurationFileMap();
                configFile.ExeConfigFilename = exeFilePath;
                Configuration config =
                    ConfigurationManager.OpenMappedExeConfiguration(configFile,
                    ConfigurationUserLevel.None);

                //System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(@"Script.config");

                int appStgCnt = ConfigurationManager.AppSettings.Count;
                string newKey = "NewKey" + appStgCnt.ToString();
                string newValue = DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString();

                config.AppSettings.Settings.Add(newKey, newValue);

                config.Save(ConfigurationSaveMode.Modified);
                ConfigurationManager.RefreshSection("appSettings");


                ConnectionStringSettings c = new ConnectionStringSettings();
                c.ConnectionString = @"ConnectionString";
                c.ProviderName = "ProviderName";
                c.Name = @"Name";

                config.ConnectionStrings.ConnectionStrings.Add(c);

                config.Save(ConfigurationSaveMode.Modified);
                ConfigurationManager.RefreshSection("connectionStrings");
            }

     

     

    2008年7月29日 6:55

全部回复

  • 是你的OpenExeConfiguration用的不对。

    我帮你改了一下,你再去试一试。

     

     

        

    Code Snippet

        private void UpdateConfigFile()
            {
                string exeFilePath = System.IO.Path.Combine(
           Environment.CurrentDirectory, @"Script.config");

                // Map to the application configuration file.
                ExeConfigurationFileMap configFile = new ExeConfigurationFileMap();
                configFile.ExeConfigFilename = exeFilePath;
                Configuration config =
                    ConfigurationManager.OpenMappedExeConfiguration(configFile,
                    ConfigurationUserLevel.None);

                //System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(@"Script.config");

                int appStgCnt = ConfigurationManager.AppSettings.Count;
                string newKey = "NewKey" + appStgCnt.ToString();
                string newValue = DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString();

                config.AppSettings.Settings.Add(newKey, newValue);

                config.Save(ConfigurationSaveMode.Modified);
                ConfigurationManager.RefreshSection("appSettings");


                ConnectionStringSettings c = new ConnectionStringSettings();
                c.ConnectionString = @"ConnectionString";
                c.ProviderName = "ProviderName";
                c.Name = @"Name";

                config.ConnectionStrings.ConnectionStrings.Add(c);

                config.Save(ConfigurationSaveMode.Modified);
                ConfigurationManager.RefreshSection("connectionStrings");
            }

     

     

    2008年7月29日 6:55
  •  

    thx.

     

    我自己后来也是这么做的,没有仔细去查询原因,但的确这样做是可以的。

     

    关键的一句话:ConfigurationManager.OpenMappedExeConfiguration(configFile, ConfigurationUserLevel.None);

     

     

    2008年7月29日 7:24