Skip to main content
 首页 » 编程设计

c#之更改设置文件运行时的值

2024年02月05日10zhwl

我正在开发一个基于 windows 的项目,在这个项目中我们使用了多个设置文件来设置控件的文本,例如 buttons.settings、lables.settings 和 .....

现在我需要在运行时用其他值更改这些设置文件的内容,因此我们创建了具有相同“名称”列但值不同的相同设置文件,现在我真的很难更改这些设置的内容文件。

我试图通过加载并将它们保存为 xmlDocument 来更改我的拖车设置文件的内容,但不幸的是我的 app.config 没有更改为新值。 我还使用了 ConfigurationManager.RefreshSection ...

请帮帮我 提前谢谢

请您参考如下方法:

我将从描述我的设置开始,以确保我们在同一页面上。

那是我的设置文件 - Settings1.settings,只有一个设置Test,默认值为 DefaultValue

此时,默认值也被复制到app.config。

现在,我有一个模板,其设置将在运行时生效,其形式为 user.config。这就是它的样子 -

这是工作实验的代码-

private void button1_Click(object sender, EventArgs e) 
    { 
        MessageBox.Show(Settings1.Default.Test); // this shows "DefaultValue" in a message box 
 
        // Now change the user.config file with our template file -  
 
        //1. I get the location of user config 
       var fileForUser = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath; 
 
        //2. now I'll Place my template file, where user.config should be present  
        
        // create directory if it doesnt exist 
        if(Directory.Exists(Path.GetDirectoryName(fileForUser)) == false)  
           Directory.CreateDirectory(Path.GetDirectoryName(fileForUser)) ;  
        
        // I have kept my template at E:\template.config 
        File.Copy(@"E:\template.config", fileForUser, true); 
       MessageBox.Show(Settings1.Default.Test); // this still shows "DefaultValue" because the user.config is not reloaded 
 
        //3. Read the new setting  
       Settings1.Default.Reload(); 
       MessageBox.Show(Settings1.Default.Test); // this shows "Default Value is changed to ABC" because the user.config is now reloaded 
 
    } 

App.config 保持原样 & incase 我删除 user.config 或调用 Settings1.Default.Reset() 然后它的 App.config 为应用程序提供默认值

希望对您有所帮助。请告诉我它是否符合您的目的。

更新 1 支持问题作者已经尝试过的方法

这是支持 yr 方法的工作代码,它将在应用程序中引入设置文件的设置 -

很遗憾我的错字 - Labels2.settings,Labels.settings 而不是 Labels2.settings & Labels.settings

        { 
            // 1. Open the settings xml file present in the same location 
            string settingName = "Lables2.SETTINGS";  // Setting file name 
            XmlDocument docSetting = new XmlDocument(); 
            docSetting.Load(Application.StartupPath + Path.DirectorySeparatorChar + settingName); 
            XmlNodeList labelSettings = docSetting.GetElementsByTagName("Settings")[0].ChildNodes; 
 
            // 2. Open the config file  
            string configFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile; 
            XmlDocument appSettingDoc = new XmlDocument(); 
            appSettingDoc.Load(configFile); 
            XmlNodeList appConfigLabelSettings = appSettingDoc.GetElementsByTagName("userSettings")[0]. 
                SelectNodes("WindowsFormsApplication2.Lables")[0].ChildNodes; 
            //ProjectName.Setting file 
 
            //3. update the config file  
            for (int i = 0; i < appConfigLabelSettings.Count; i++) 
            { 
                var v = appConfigLabelSettings.Item(i).ChildNodes[0]; 
                v.InnerText = labelSettings.Item(i).InnerText; 
            } 
 
            //4. save & load the settings  
            appSettingDoc.Save(configFile); 
            Lables.Default.Reload(); 
 
 
            MessageBox.Show(Lables.Default.Code); // test pass... shows A2 
        } 

我的项目设置 -

这是可执行文件夹,其中

这就是 labels2.settings 的样子

没有xml文档的更新2方法

所有的设置都是一样的,而且更简洁。请尝试-

 { 
 
        // 1. Open the settings xml file present in the same location 
        string settingName = "Lables2.SETTINGS";  // Setting file name 
        XmlDocument docSetting = new XmlDocument(); 
        docSetting.Load(Application.StartupPath + Path.DirectorySeparatorChar + settingName); 
        XmlNodeList labelSettings = docSetting.GetElementsByTagName("Settings")[0].ChildNodes; 
 
 
        Console.WriteLine("Code {0} Group{1} Name{2}", Lables.Default.Code, Lables.Default.Group, Lables.Default.Name); //prints Code A1 GroupB1 NameC1 
 
 
        //2. look for all Lables2 settings in Label settings & update 
         foreach (XmlNode item in labelSettings) 
         { 
             var nameItem = item.Attributes["Name"]; 
             Lables.Default.PropertyValues[nameItem.Value].PropertyValue = item.InnerText;                 
         } 
 
         Lables.Default.Save(); // save. this will save it to user.config not app.config but the setting will come in effect in application 
         Lables.Default.Reload(); 
 
         Console.WriteLine("Code {0} Group{1} Name{2}", Lables.Default.Code, Lables.Default.Group, Lables.Default.Name); //prints Code A2 GroupB2 NameC2 
 
    }