我面临的一个问题。
我想保存在app.config文件设置
我写了单独的类,并在配置文件中定义的部分..
但是当我运行应用程序。 它不保存给定值到配置文件
这里是SettingsClass
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>
u能告诉我哪里是错误..我想了很多,从互联网上阅读。 但仍无法保存配置文件信息..
我跑的exe文件的应用程序双击也。
按照MSDN:ConfigurationManager.GetSection方法 ,
该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轻松检查结果。
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="MySettings" type="TestApp.MySettings, TestApp"/>
</configSections>
<MySettings CustomerName="" EmailAddress="" TimeStamp=""/>
</configuration>
在许多情况下(在限制用户方案)的用户不具有直接写入访问应用程序的配置文件。 来解决这个问题,你需要使用usersettings。
如果用鼠标右键单击您的项目,并选择名为“设置”选项卡,你可以使所存放的配置文件的设置列表。 如果选择“范围”为“用户”,设置使用的类型,它会自动存储用户AppData的区域,用户八方通有写权限下的设置自动存储在配置文件中。 这些设置也被自动在属性\ Settings.Designer.cs代码文件创建的属性提供的,而在你的代码访问Properties.Settings.Default
。
例:
比方说,你添加一个名为客户名称的用户设置:
在加载应用程序,你会想从存储的设置retreive的值(默认值,因为它是在应用程序配置,或者如果为该用户存储,为用户配置文件):
string value = Properties.Settings.Default.CustomerName;
如果你想改变的价值,只写它:
Properties.Settings.Default.CustomerName = "John Doe";
当您要保存所有设置,只需调用保存:
Properties.Settings.Default.Save();
请注意,当你开发,用户文件将被偶尔重置为默认,但构建应用程序时,这只是发生。 当你刚刚运行的应用程序,你存储的设置会从应用程序的用户配置文件读取。
如果你仍然想创建自己的设置操作,你可以试试这个一次,看看有VisualStudio中自动创建为你了解你需要得到这个工作的概念。
为了实际更新配置文件,您将需要调用.Save()
上的Configuration
对象-不只是你的配置节对象。
你应该检查出乔恩Rista的三部系列在.NET 2.0配置了CodeProject上。
- 解开的.NET 2.0配置的奥秘
- 解码的.NET 2.0配置的奥秘
- 裂化的.NET 2.0配置的奥秘
强烈推荐,写得很好,非常有帮助! 它显示了所有的插件和处理.NET 2.0及以上配置的出局,并帮助我非常多获得关于这个问题的把握。
渣
请注意,appname.vshost.exe.Config回复到它在调试会话结束时的原始状态。 所以,你可能会被保存到文件(您可以通过使用在执行过程中的记事本查看),然后失去了保存内容的调试停止时。