保存到 XML 文件不保存 - WPF
Save to XML File Does not Save - WPF
当我去保存到我的 XML 文件时,输入了所有正确的值并且我的代码显示 运行 顺利,正确的 App.config value 被保存到正确的 App.config key,但是当我去保存到我的 XML 文件时它不起作用。
我的代码在这里
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 中打开我的 App.config 文件时,没有进行任何更改,但是当我从我的配置文件中读取时,
using System.Configuration;
var Variable = ConfigurationManager.AppSettings.Get("Directory");
已进行正确的更改。
希望这能帮助遇到与我相同问题的人:)
当我去保存到我的 XML 文件时,输入了所有正确的值并且我的代码显示 运行 顺利,正确的 App.config value 被保存到正确的 App.config key,但是当我去保存到我的 XML 文件时它不起作用。
我的代码在这里
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 中打开我的 App.config 文件时,没有进行任何更改,但是当我从我的配置文件中读取时,
using System.Configuration;
var Variable = ConfigurationManager.AppSettings.Get("Directory");
已进行正确的更改。
希望这能帮助遇到与我相同问题的人:)