C# 如何在 WPF 应用程序中使用 App.config 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/806174/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use a App.config file in WPF applications?
提问by Edward Tanguay
I created a App.config file in my WPF application:
我在 WPF 应用程序中创建了一个 App.config 文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appsettings>
<add key="xmlDataDirectory" value="c:\testdata"/>
</appsettings>
</configuration>
Then I try to read the value out with this:
然后我尝试用这个读取值:
string xmlDataDirectory = ConfigurationSettings.AppSettings.Get("xmlDataDirectory");
But it says this is obsolete and that I should use ConfigurationManager which I can't find, even searching in the class view.
但它说这已经过时了,我应该使用我找不到的 ConfigurationManager,甚至在类视图中搜索。
Does anyone know how to use config files like this in WPF?
有谁知道如何在 WPF 中使用这样的配置文件?
采纳答案by Cédric Rup
You have to reference the System.Configuration
assembly that is in GAC.
您必须引用System.Configuration
GAC 中的程序集。
Use of ConfigurationManager
is not WPF-specific: it is the privileged way to access configuration information for any type of application.
的使用ConfigurationManager
不是 WPF 特定的:它是访问任何类型应用程序配置信息的特权方式。
Please see MSDNfor further info
请参阅MSDN了解更多信息
Hope it helps !
希望能帮助到你 !
Cédric
塞德里克
回答by Anand Shah
In your app.config
, change your appsetting to
在您的 中app.config
,将您的 appsetting 更改为
<applicationSettings>
<WpfApplication1.Properties.Settings>
<setting name="appsetting" serializeAs="String">
<value>c:\testdata.xml</value>
</setting>
</WpfApplication1.Properties.Settings>
</applicationSettings>
Then, in the code-behind,
然后,在代码隐藏中,
string xmlDataDirectory = WpfApplication1.Properties.Settings.Default.appsetting.ToString()
HTH
HTH
回答by mehran
This also works
这也有效
WpfApplication1.Properties.Settings.Default["appsetting"].ToString()
回答by Larry
You have to add the reference to System.configuration
in your solution. Also, include using System.Configuration;
. Once you do that, you'll have access to all the configuration settings.
您必须System.configuration
在解决方案中添加对的引用。此外,包括using System.Configuration;
. 完成此操作后,您将可以访问所有配置设置。
回答by user1210085
You have to reference System.Configuration
via explorer (not only append using System.Configuration
). Then you can write:
您必须System.Configuration
通过资源管理器进行引用(不仅是 append using System.Configuration
)。然后你可以写:
string xmlDataDirectory =
System.Configuration.ConfigurationManager.AppSettings.Get("xmlDataDirectory");
Tested with VS2010 (thanks to www.developpez.net). Hope this helps.
用 VS2010 测试(感谢 www.developpez.net)。希望这可以帮助。
回答by Vitaly Asher Novitsky
You can change configuration file schema back to DotNetConfig.xsd
via properties of the app.config
file. To find destination of needed schema, you can search it by name or create a WinForms application, add to project the configuration file and in it's properties, you'll find full path to file.
您可以DotNetConfig.xsd
通过app.config
文件的属性将配置文件架构更改回。要查找所需架构的目标,您可以按名称搜索它或创建一个 WinForms 应用程序,将配置文件添加到项目中,并在其属性中找到文件的完整路径。
回答by Ziggler
In my case I followed below steps
就我而言,我遵循以下步骤
App.config
应用配置
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<appSettings>
<add key="POCPublishSubscribeQueueName" value="FormatName:Direct=OS:localhost\Private$\POCPublishSubscribe"/>
</appSettings>
</configuration>
Added System.Configuartion to my project
将 System.Configuartion 添加到我的项目中
Added using System.Configuration statement in file at top
在顶部的文件中添加 using System.Configuration 语句
Then used below statement
然后使用下面的语句
string queuePath = ConfigurationManager.AppSettings["POCPublishSubscribeQueueName"].ToString();
回答by RonaldPaguay
I have a Class Library WPF Project, and I Use:
我有一个类库 WPF 项目,我使用:
'Read Settings
Dim value as string = My.Settings.my_key
value = "new value"
'Write Settings
My.Settings.my_key = value
My.Settings.Save()