How to use a App.config file in WPF applications?

前端 未结 8 2139
既然无缘
既然无缘 2020-12-04 13:59

I created an App.config file in my WPF application:


         


        
相关标签:
8条回答
  • 2020-12-04 14:26

    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.

    0 讨论(0)
  • 2020-12-04 14:29

    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()
    
    0 讨论(0)
  • 2020-12-04 14:29

    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.

    0 讨论(0)
  • 2020-12-04 14:31

    In my case, I followed the steps below.

    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.

    Added using System.Configuration statement in file at top.

    Then used this statement:

    string queuePath = ConfigurationManager.AppSettings["POCPublishSubscribeQueueName"].ToString();
    
    0 讨论(0)
  • 2020-12-04 14:36

    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.

    0 讨论(0)
  • 2020-12-04 14:39

    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()
    
    0 讨论(0)
提交回复
热议问题