gpt4 book ai didi

c# - 无法在 app.exe.config 中保存设置

转载 作者:太空狗 更新时间:2023-10-29 20:06:33 24 4
gpt4 key购买 nike

我面临一个问题。

我想在 app.config 文件中保存设置

我在配置文件中写了单独的类和定义的部分..

但是当我运行应用程序时。它不会将给定值保存到配置文件中

这里是设置类

public class MySetting:ConfigurationSection
{
private static MySetting settings = ConfigurationManager.GetSection("MySetting") as MySetting;

public override bool IsReadOnly()
{
return false;
}

public static MySetting Settings
{
get
{
return settings;
}
}


[ConfigurationProperty("CustomerName")]
public String CustomerName
{
get
{
return settings["CustomerName"].ToString();
}
set
{
settings["CustomerName"] = value;
}
}


[ConfigurationProperty("EmailAddress")]
public String EmailAddress
{
get
{
return settings["EmailAddress"].ToString();
}
set
{
settings["EmailAddress"] = value;
}
}


public static bool Save()
{
try
{
System.Configuration.Configuration configFile = Utility.GetConfigFile();
MySetting mySetting = (MySetting )configFile.Sections["MySetting "];

if (null != mySetting )
{
mySetting .CustomerName = settings["CustomerName"] as string;
mySetting .EmailAddress = settings["EmailAddress"] as string;

configFile.Save(ConfigurationSaveMode.Full);
return true;
}

return false;
}
catch
{
return false;
}
}
}

这是我在配置文件中保存信息的代码

private void SaveCustomerInfoToConfig(String name, String emailAddress)
{

MySetting .Settings.CustomerName = name;
MySetting .Settings.EmailAddress = emailAddress
MySetting .Save();
}

这是app.config

<configuration>
<configSections>
<section name="MySettings" type="TestApp.MySettings, TestApp"/>
</configSections>

<MySettings CustomerName="" EmailAddress="" />
</configuration>

你能告诉我错误在哪里吗..我尝试了很多并从互联网上阅读。但仍然无法在配置文件中保存信息..

我还通过双击 exe 文件来运行应用程序。

最佳答案

根据MSDN: ConfigurationManager.GetSection Method ,

ConfigurationManager.GetSection方法访问运行时配置信息它无法更改。要更改配置,请使用 Configuration.GetSection使用以下打开方法之一获取的配置文件上的方法:

但是,如果你想更新 app.config 文件,我会把它作为一个 xml 文档来读取,并像一个普通的 xml 文档一样操作它。

请看下面的例子:注意:此示例仅用于概念验证。不应按原样用于生产。

using System;
using System.Linq;
using System.Xml.Linq;

namespace ChangeAppConfig
{
class Program
{
static void Main(string[] args)
{
MyConfigSetting.CustomerName = "MyCustomer";
MyConfigSetting.EmailAddress = "MyCustomer@Company.com";
MyConfigSetting.TimeStamp = DateTime.Now;
MyConfigSetting.Save();
}
}

//Note: This is a proof-of-concept sample and
//should not be used in production as it is.
// For example, this is not thread-safe.
public class MyConfigSetting
{
private static string _CustomerName;
public static string CustomerName
{
get { return _CustomerName; }
set
{
_CustomerName = value;
}
}

private static string _EmailAddress;
public static string EmailAddress
{
get { return _EmailAddress; }
set
{
_EmailAddress = value;
}
}

private static DateTime _TimeStamp;
public static DateTime TimeStamp
{
get { return _TimeStamp; }
set
{
_TimeStamp = value;
}
}

public static void Save()
{
XElement myAppConfigFile = XElement.Load(Utility.GetConfigFileName());
var mySetting = (from p in myAppConfigFile.Elements("MySettings")
select p).FirstOrDefault();
mySetting.Attribute("CustomerName").Value = CustomerName;
mySetting.Attribute("EmailAddress").Value = EmailAddress;
mySetting.Attribute("TimeStamp").Value = TimeStamp.ToString();

myAppConfigFile.Save(Utility.GetConfigFileName());

}
}

class Utility
{
//Note: This is a proof-of-concept and very naive code.
//Shouldn't be used in production as it is.
//For example, no null reference checking, no file existence checking and etc.
public static string GetConfigFileName()
{
const string STR_Vshostexe = ".vshost.exe";
string appName = Environment.GetCommandLineArgs()[0];

//In case this is running under debugger.
if (appName.EndsWith(STR_Vshostexe))
{
appName = appName.Remove(appName.LastIndexOf(STR_Vshostexe), STR_Vshostexe.Length) + ".exe";
}

return appName + ".config";
}
}
}

我还在 app.config 中的 MySettings 添加了“TimeStamp”属性,以便轻松检查结果。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="MySettings" type="TestApp.MySettings, TestApp"/>
</configSections>

<MySettings CustomerName="" EmailAddress="" TimeStamp=""/>
</configuration>

关于c# - 无法在 app.exe.config 中保存设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1157378/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com