tags:

views:

127

answers:

4

Ideally I'd be using files of the same basic structure as an app.config and pulling settings buy so long as I can get any sort of "pathed" XML storage for settings I'd be okay.

Ideally given something like :

<Root>
 <ApplicationSettings>
  <Setting key="SomeKey", value="SomeValue"/>
 </ApplicationSettings>
</Root>

I'd call something like:

Settings.Get("ApplicationSettings.SomeKey");

and get "SomeValue" as an lresult.

Anyone know of any easy standard commonly in place way to do this?

+3  A: 

I'm not sure if I understand the question, as you seem to be familiar with the App.Config file... There is a method very similar to what you posted. Create an App.Config file for your project, and put the following code in it:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <appSettings>
        <add key="Key1" value="Value1" />
        <add key="Key2" value="Value2" />
    </appSettings>
</configuration>

And then you can access it in your code using:

string key1= ConfigurationSettings.AppSettings["Key1"];
froadie
but I don't want to use the literal App.Config seeing as allowing it to change is against best practice.
Firoso
@firoso - I think I'm missing something about your question... What do you need to use the App.Config file for? The App.Config file is the standard way to save configuration settings in a .NET project, for use throughout the project.
froadie
what do you mean allowing it to change is against best practice? I'd assume you mean editing the web.config is against best pratice, but you can keep static values in web.config and reference additional values in an external app.config (see http://weblogs.asp.net/pwilson/archive/2003/04/09/5261.aspx) The only value I see in your question as posted is the ability to add many different files and/or configuration sections.
Jim Schubert
he's saying "same basic structure as app.config", not necessarily THE app.config.
rockinthesixstring
ConfigurationSettings.AppSettings works if the settings are in the app.config/web.config, an included config, the machine.config the machine's web.config, or whatever.
brianary
A: 

You can build a function that pulls the Value. I have done this

 Public Class Settings
     Public Function GetSetting(Byval SettingsKey As String) As String

        Dim xDoc As New XmlDocument
        xDoc.Load("app.config")
        Dim xNode As XmlNode
        xNode = xDoc.SelectSingleNode(("Settings/setting[@item='" & SettingsKey & "']"))
        If xNode Is Nothing Then
            Return String.Empty
        Else
            Return xNode.Attributes("value").Value
        End If
        xDoc = Nothing

    End Sub
End Class

And then call it like this

Settings.GetSetting("MyArbitraryKey")
rockinthesixstring
I love getting down votes when my answer's not wrong.
rockinthesixstring
+2  A: 

Yes.

A long time ago, Craig Andera has described the last configuration handler you'll ever need. It uses app.config to provide the configuration. You could modify it to get the config from somewhere else.

Cheeso
+1  A: 

The standard is to use app.config/web.config.

I have no idea where you heard that changing it is not best practice - I think someone is trying to fool you.

John Saunders