可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I created a App.config file in my WPF application:
<?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.
Does anyone know how to use config files like this in WPF?
回答1:
You have to reference the System.Configuration
assembly that is in GAC.
Use of ConfigurationManager
is not WPF-specific: it is the privileged way to access configuration information for any type of application.
Please see MSDN for further info
Hope it helps !
Cédric
回答2:
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 solution
Added using System.Configuration statement in file at top
Then used below statement
string queuePath = ConfigurationManager.AppSettings["POCPublishSubscribeQueueName"].ToString();
回答3:
In your app.config
, change your appsetting to
<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
回答4:
You have to reference System.Configuration
via explorer (not only append using System.Configuration
). Then you can write:
string xmlDataDirectory =
System.Configuration.ConfigurationManager.AppSettings.Get("xmlDataDirectory");
Tested with VS2010 (thanks to www.developpez.net).
Hope this helps.
回答5:
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.
回答6:
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.
回答7:
This also works
WpfApplication1.Properties.Settings.Default["appsetting"].ToString()
回答8:
I have a Class Library WPF Project, and I Use:
'Read Settings
Dim value as string = My.Settings.my_key
value = "new value"
'Write Settings
My.Settings.my_key = value
My.Settings.Save()