我正在开发一个基于Windows的项目,在这个项目中我们使用多个设置文件来设置控件的文本,例如buttons.settings,lables.settings和.....
现在我需要在运行时使用其他值更改这些设置文件的内容,因此我们创建了具有相同列“名称”但不同值的相同设置文件,现在我真的遇到了更改这些设置内容的问题文件。
我尝试通过加载并将其保存为xmlDocument来更改我的拖动设置文件的内容,但不幸的是我的app.config不会被新值更改。 我还使用了ConfigurationManager.RefreshSection ...
请帮助我 thnx提前
答案 0 :(得分:2)
我将从描述我的设置开始,以确保我们在同一页面上。
这就是我的设置文件 - 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保持原样&我删除user.config或调用Settings1.Default.Reset()
然后调用App.config,它为应用程序提供默认值
希望它有所帮助。如果它符合你的目的,请告诉我。
以下是支持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
}
这就是labels2.settings的样子
所有设置都相同&amp;这更清洁。请尝试 -
{
// 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
}
答案 1 :(得分:1)
XmlDocument doc = new XmlDocument();
//doc.Load(@"C:\Users\***\Documents\Visual Studio 2008\Projects\ChangingLablesRuntime\ChangingLablesRuntime\_Labels2.settings");
//doc.Save(@"C:\Users\SHYAZDI.IDEALSYSTEM\Documents\Visual Studio 2008\Projects\ChangingLablesRuntime\ChangingLablesRuntime\_Labels.settings");
doc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
var root = doc.GetElementsByTagName("userSettings")[0];
doc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
var Config = System.Configuration.ConfigurationManager.OpenExeConfiguration(@"path of app.config");
var root = doc.GetElementsByTagName("userSettings")[0];
doc.GetElementsByTagName("userSettings")[0].SelectSingleNode("Zeus._Labels").InnerText = doc.GetElementsByTagName("userSettings")[0].SelectSingleNode("ChangingLablesRuntime._Labels2").InnerText;
//var newEml = root.SelectSingleNode("ChangingLablesRuntime._Labels2");
//var oldEml = root.SelectSingleNode("Zeus._Labels");
//oldEml.InnerText = newEml.InnerText;
//oldEml.ParentNode.ReplaceChild(newEml, oldEml);
doc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
Config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("userSettings");
这是我的代码,lables2与lables1相同,具有不同的值,运行此代码后没有任何反复发生。
这是lables1.settings,我想用lables2.settings替换: http://threejs.org/docs/#Reference/Math/Matrix4 这是lables2.settings:
答案 2 :(得分:1)
这可能是xmlDocument的问题,如Changing App.config at Runtime
所述请保持设置与我上次对label.settings&amp;的回复相同。 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
}
它对我有用,&amp;因为它没有xmldocument,我希望它也能在你的工作结束。请告诉我结果。