保存到XML文件不保存 - WPF
问题描述:
当我保存到我的XML文件时,输入所有正确的值,并且我的代码看起来运行平稳,正确的App.config值被保存到正确的App.config键,但是当我去保存到我的XML文件它不起作用。保存到XML文件不保存 - WPF
我的代码是在这里
的App.config:
<<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="Test" value="Entry"/>
<add key="Directory" value="WYNW"/>
</appSettings>
<*More stuff*>
我习惯叫我救
private void btnDirectory_Click(object sender, RoutedEventArgs e)
{
string oldDirectory = btnDirectory.Content.ToString();
string newDirectory = grabFolder(); //Grab Folder just grabs the folder location as a string
if (!(string.IsNullOrWhiteSpace(newDirectory)))
{
btnDirectory.Content = newDirectory;
changeValue(KeyValue[1], oldDirectory, newDirectory); //KeyValue[1] is "Directory"
}
}
我用来保存到XML
public void changeValue(string Key_Value, string Old_Value, string New_Value)
{
MessageBoxResult result = MessageBox.Show("Change " + Old_Value + " to " + New_Value + " for " + Key_Value, "Sales Vault Notifer", MessageBoxButton.YesNo);
switch (result)
{
case MessageBoxResult.Yes:
{
XmlDocument XMLdoc = new XmlDocument();
XMLdoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
foreach (XmlElement element in XMLdoc.DocumentElement)
{
if (element.Name == "appSettings")
{
foreach (XmlNode node in element.ChildNodes)
{
if (node.Attributes[0].Value == Key_Value)
{
node.Attributes[1].Value = New_Value;
MessageBox.Show(node.Attributes[1].Value); //Check to make sure that correct key-value has been changed ... It shows that it changes to what directory I want
}
}
}
}
XMLdoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
break;
}
}
}
一切工作顺利,直到我到了XMLdoc.Save()部分。我在C#应用程序中使用了该代码,并且它工作的很顺利,但是当我切换到WPF时,MessageBoxes的Yes/No部分与我所能找到的不同并且是必需的(从我所能找到的)开始检查,所以我假设break;是我无法保存的原因。
任何帮助,将不胜感激:)
干杯, 亚托
答
试错后,我来到了结论:
该文件被保存,只是它一直保存到
//MyProject/bin/debugg/MyProject.exe.config
,而不是我在那里手工编辑的
//MyProject/App.config
由于我的配置文件,通过slugster解释here
因此,当我去开我的应用程序。配置文件在Visual Studio中没有更改,但是当我从我的配置文件中读取时,
using System.Configuration;
var Variable = ConfigurationManager.AppSettings.Get("Directory");
已经做出了正确的改变。
希望这可以帮助谁有同样的问题,我有:)