C#4.0 - 如何创建&使用VS2010 SP1的项目配置

时间:2011-05-28 13:26:02

标签: c#-4.0

我需要创建一个项目配置文件(即project.config或project.ini),以便我以后可以使用它来存储用户选择的首选项。当应用程序启动时,它将自动加载该配置文件并根据以前的设置自定义应用程序。

在C#4.0中有一个很好的方法吗? 我曾经通过在C ++中操作文件来手动完成所有这些工作,我期望用.NET获得更清晰,更简单的解决方案。

谢谢

2 个答案:

答案 0 :(得分:1)

不仅在C#中,XML技术对这类任务很有用。您可以创建描述配置文件格式的XML Schema,然后可以使用xsd.exe从此模式生成C#类,然后使用几行代码从xml文件序列化/反序列化。 / p>

答案 1 :(得分:1)

对于WinForms和WPF,请考虑使用内置的Application Settings。在这种情况下,无需自己动手。这是一种代表您的应用程序和用户创建和存储设置数据的方法。

  • 预定义您希望用户能够保存的设置。我们说它是WindowSizeBackgroundImage

  • 像这样访问这些属性:

//get the value of WindowSize which is a string  
string windowSize = Properties.Settings.Default.WindowSize;    
MessageBox.Show("WindowSize setting value is :" + windowSize );  

//set the new value of BackgroundImage
Properties.Settings.Default.BackgroundImage= "http://foo.org/bar.png";  
Properties.Settings.Default.Save();      //apply the changes to the settings file  

这持续到yourAppName.exe.config

相关问题