C# 在运行时更改设置文件的值

C# 在运行时更改设置文件的值,c#,settings,app-config,multilingual,C#,Settings,App Config,Multilingual,我正在从事一个基于windows的项目,在这个项目中,我们使用了多个设置文件来设置控件的文本,例如按钮.设置,标签.设置和 现在我需要在运行时使用其他值更改这些设置文件的内容,因此我们使用相同的列“名称”创建了相同的设置文件,但值不同,现在我确实无法更改这些设置文件的内容 我试图通过加载并保存为xmlDocument来更改我的两个设置文件的内容,但不幸的是,我的app.config没有通过新值进行更改。 我还使用了ConfigurationManager.RefreshSection 请帮帮我

我正在从事一个基于windows的项目,在这个项目中,我们使用了多个设置文件来设置控件的文本,例如按钮.设置,标签.设置和

现在我需要在运行时使用其他值更改这些设置文件的内容,因此我们使用相同的列“名称”创建了相同的设置文件,但值不同,现在我确实无法更改这些设置文件的内容

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

请帮帮我
thnx提前

我将从描述我的设置开始,只是为了确保我们在同一页上

这是我的设置文件Settings1.settings,只有一个设置测试,默认值是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保持原样&如果我删除user.config或调用
Settings1.Default.Reset()
则是为应用程序提供默认值的App.config

希望能有帮助。一定要让我知道这是否符合你的目的

更新1支持问题作者已经尝试过的方法 下面是支持yr方法的工作代码,它将在应用程序中引入设置文件的设置-

很抱歉我输入错误-Lables2.settings,Lables.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
        }

我将从描述我的设置开始,只是为了确保我们在同一页上

这是我的设置文件Settings1.settings,只有一个设置测试,默认值是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保持原样&如果我删除user.config或调用
Settings1.Default.Reset()
则是为应用程序提供默认值的App.config

希望能有帮助。一定要让我知道这是否符合你的目的

更新1支持问题作者已经尝试过的方法 下面是支持yr方法的工作代码,它将在应用程序中引入设置文件的设置-

很抱歉我输入错误-Lables2.settings,Lables.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
        }
这是我的代码,lables2和lables1具有不同的值,在运行这段代码之后,什么都没有发生

这是一段lables1.settings,我想用lables2.settings替换它: 这是一张标签2.1设置:

和app.config相关代码:

这是我的代码,lables2和lables1具有不同的值,在运行这段代码之后,什么都没有发生

这是一段lables1.settings,我想用lables2.settings替换它: 这是一张标签2.1设置:

和app.config相关代码:


可能是这里提到的xmlDocument的问题

请保持设置与我上次对label.settings和label2.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;


        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

    }

它适合我,因为它没有xmldocument,我希望它在年底也能工作。一定要告诉我结果。

可能是这里提到的xmlDocument的问题

请保持设置与我上次对label.settings和label2.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;


        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

    }

它适合我,因为它没有xmldocument,我希望它在年底也能工作。一定要告诉我结果。

您使用的是winforms吗?还有一个问题,您希望更改的设置在应用程序运行时生效吗?或者在重新启动应用程序后?是,我正在使用winform。。。。。是的,当用户登录应用程序时,我想在运行时更改它。我将检查他的硬件锁,然后通过检查用户的版本类型,我决定在我的主设置(labels.setting)中加载哪个设置内容(例如labels2.setting)。我可以通过XML加载和保存mathod来更改设置内容,但是我的app.config没有得到更改。我删除了我的答案,因为我想我误解了这是一个本地化问题。不管怎样,您能告诉我您在*.settings文件中定义的设置范围是什么吗?是it-user吗?还是应用程序?。如果是用户范围,则要使设置生效,只需调用
Settings1.Default.Reload()更改user.config后。(
Settings1
是Visual Studio在添加Settings1.Settings文件时自动创建的
ApplicationSettingsBase
),这不会更改app.config,但更改的值将从user.config文件生效。对于您的帮助,我使用的是user scope。我想先更改设置文件,然后我希望我的app.config生效,但它不会更改!我坚持先更改设置文件,因为我想用另一个设置文件的值更改设置文件中的所有值,而不仅仅是更改一些值!我现在明白你的安排了。原则上,app.config完全可以不受用户范围设置的影响。此行为应使每个用户都可以拥有自己的设置。如果用户未更改设置,将使用app.config中的默认值。是否使用winforms?还有一个问题,您希望更改的设置在应用程序运行时生效吗?或者在重新启动应用程序后?是,我正在使用winform。。。。。是的,我想在跑步时更换