Skip to main content
added 1 character in body
Source Link
Heslacher
  • 51k
  • 5
  • 83
  • 177

While somewhat of a tangent from your question but you may find it helpful nonetheless.

I would recommend looking at a custom ConfigurationSection which allows you to define more complex configuration hierarchies that are strongly-typed. I find it much easier to use a custom configuration section than having to remember a bunch of magic strings in the appSettings element and also allows you to specific which values are required, what the defaults values are, etc.

Using a custom configuration section you could create a configuration type like:

public class UboldiConfigurationSection : System.Configuration.ConfigurationSection {
    [ConfigurationProperty("schoolName")]
    public string SchoolName {
        get { return (string)this["schoolName"]; }
        set { this["schoolName"] = value; }
    }
}

Then to load that configuration type:

public static class UboldiApplcationUboldiApplication {
    public static UboldiConfigurationSection Config { get; internal set; }

    public static void Initialize() {
        Config = ConfigurationManager.GetSection("uboldi") as UboldiConfigurationSection;
    }
}

The app.config then would look something like this:

<configuration>
    <configSections>
        <section name="uboldi" type="Uboldi.UboldiConfigurationSection, Uboldi" />
    </configSections>
    <uboldi schoolName="Fillmore Central" />
</configuration>

Lastly, you use the configuration by:

public void Test() {    
    //This only needs to be done once, presumably in your Program.Main method
    UboldiApplication.Initialize();
    
    var name = UboldiApplication.Config.SchoolName;
}

A couple of notes:

  1. You'll need to reference the System.Configuration assembly as it's not usually referenced in VS by default.
  2. The ConfigurationManager.GetSection("uboldi") is expecting the name of the section in the app.config file. You'll note that this matches in the example above.
  3. The section element in the app.config file uses the standard .Net type name convention to locate the specified configuration section. In this example I am assuming that the UboldiConfigurationSection type is the Uboldi namespace and in an Uboldi assembly (dll or exe).
  4. You can add hierarchy by creating ConfigurationElement sub classes and using them as properties in your configuration section and elements.
  5. The link above is for a Web.config, but the same thing is possible in an app.config file.

While somewhat of a tangent from your question but you may find it helpful nonetheless.

I would recommend looking at a custom ConfigurationSection which allows you to define more complex configuration hierarchies that are strongly-typed. I find it much easier to use a custom configuration section than having to remember a bunch of magic strings in the appSettings element and also allows you to specific which values are required, what the defaults values are, etc.

Using a custom configuration section you could create a configuration type like:

public class UboldiConfigurationSection : System.Configuration.ConfigurationSection {
    [ConfigurationProperty("schoolName")]
    public string SchoolName {
        get { return (string)this["schoolName"]; }
        set { this["schoolName"] = value; }
    }
}

Then to load that configuration type:

public static class UboldiApplcation {
    public static UboldiConfigurationSection Config { get; internal set; }

    public static void Initialize() {
        Config = ConfigurationManager.GetSection("uboldi") as UboldiConfigurationSection;
    }
}

The app.config then would look something like this:

<configuration>
    <configSections>
        <section name="uboldi" type="Uboldi.UboldiConfigurationSection, Uboldi" />
    </configSections>
    <uboldi schoolName="Fillmore Central" />
</configuration>

Lastly, you use the configuration by:

public void Test() {    
    //This only needs to be done once, presumably in your Program.Main method
    UboldiApplication.Initialize();
    
    var name = UboldiApplication.Config.SchoolName;
}

A couple of notes:

  1. You'll need to reference the System.Configuration assembly as it's not usually referenced in VS by default.
  2. The ConfigurationManager.GetSection("uboldi") is expecting the name of the section in the app.config file. You'll note that this matches in the example above.
  3. The section element in the app.config file uses the standard .Net type name convention to locate the specified configuration section. In this example I am assuming that the UboldiConfigurationSection type is the Uboldi namespace and in an Uboldi assembly (dll or exe).
  4. You can add hierarchy by creating ConfigurationElement sub classes and using them as properties in your configuration section and elements.
  5. The link above is for a Web.config, but the same thing is possible in an app.config file.

While somewhat of a tangent from your question but you may find it helpful nonetheless.

I would recommend looking at a custom ConfigurationSection which allows you to define more complex configuration hierarchies that are strongly-typed. I find it much easier to use a custom configuration section than having to remember a bunch of magic strings in the appSettings element and also allows you to specific which values are required, what the defaults values are, etc.

Using a custom configuration section you could create a configuration type like:

public class UboldiConfigurationSection : System.Configuration.ConfigurationSection {
    [ConfigurationProperty("schoolName")]
    public string SchoolName {
        get { return (string)this["schoolName"]; }
        set { this["schoolName"] = value; }
    }
}

Then to load that configuration type:

public static class UboldiApplication {
    public static UboldiConfigurationSection Config { get; internal set; }

    public static void Initialize() {
        Config = ConfigurationManager.GetSection("uboldi") as UboldiConfigurationSection;
    }
}

The app.config then would look something like this:

<configuration>
    <configSections>
        <section name="uboldi" type="Uboldi.UboldiConfigurationSection, Uboldi" />
    </configSections>
    <uboldi schoolName="Fillmore Central" />
</configuration>

Lastly, you use the configuration by:

public void Test() {    
    //This only needs to be done once, presumably in your Program.Main method
    UboldiApplication.Initialize();
    
    var name = UboldiApplication.Config.SchoolName;
}

A couple of notes:

  1. You'll need to reference the System.Configuration assembly as it's not usually referenced in VS by default.
  2. The ConfigurationManager.GetSection("uboldi") is expecting the name of the section in the app.config file. You'll note that this matches in the example above.
  3. The section element in the app.config file uses the standard .Net type name convention to locate the specified configuration section. In this example I am assuming that the UboldiConfigurationSection type is the Uboldi namespace and in an Uboldi assembly (dll or exe).
  4. You can add hierarchy by creating ConfigurationElement sub classes and using them as properties in your configuration section and elements.
  5. The link above is for a Web.config, but the same thing is possible in an app.config file.
Changed "client" accessor to "schoolName" because that was the configuration property attempting to be accessed.
Source Link

While somewhat of a tangent from your question but you may find it helpful nonetheless.

I would recommend looking at a custom ConfigurationSection which allows you to define more complex configuration hierarchies that are strongly-typed. I find it much easier to use a custom configuration section than having to remember a bunch of magic strings in the appSettings element and also allows you to specific which values are required, what the defaults values are, etc.

Using a custom configuration section you could create a configuration type like:

public class UboldiConfigurationSection : System.Configuration.ConfigurationSection {
    [ConfigurationProperty("schoolName")]
    public string SchoolName {
        get { return (string)this["client"];this["schoolName"]; }
        set { this["client"]this["schoolName"] = value; }
    }
}

Then to load that configuration type:

public static class UboldiApplcation {
    public static UboldiConfigurationSection Config { get; internal set; }

    public static void Initialize() {
        Config = ConfigurationManager.GetSection("uboldi") as UboldiConfigurationSection;
    }
}

The app.config then would look something like this:

<configuration>
    <configSections>
        <section name="uboldi" type="Uboldi.UboldiConfigurationSection, Uboldi" />
    </configSections>
    <uboldi schoolName="Fillmore Central" />
</configuration>

Lastly, you use the configuration by:

public void Test() {    
    //This only needs to be done once, presumably in your Program.Main method
    UboldiApplication.Initialize();
    
    var name = UboldiApplication.Config.SchoolName;
}

A couple of notes:

  1. You'll need to reference the System.Configuration assembly as it's not usually referenced in VS by default.
  2. The ConfigurationManager.GetSection("uboldi") is expecting the name of the section in the app.config file. You'll note that this matches in the example above.
  3. The section element in the app.config file uses the standard .Net type name convention to locate the specified configuration section. In this example I am assuming that the UboldiConfigurationSection type is the Uboldi namespace and in an Uboldi assembly (dll or exe).
  4. You can add hierarchy by creating ConfigurationElement sub classes and using them as properties in your configuration section and elements.
  5. The link above is for a Web.config, but the same thing is possible in an app.config file.

While somewhat of a tangent from your question but you may find it helpful nonetheless.

I would recommend looking at a custom ConfigurationSection which allows you to define more complex configuration hierarchies that are strongly-typed. I find it much easier to use a custom configuration section than having to remember a bunch of magic strings in the appSettings element and also allows you to specific which values are required, what the defaults values are, etc.

Using a custom configuration section you could create a configuration type like:

public class UboldiConfigurationSection : System.Configuration.ConfigurationSection {
    [ConfigurationProperty("schoolName")]
    public string SchoolName {
        get { return (string)this["client"]; }
        set { this["client"] = value; }
    }
}

Then to load that configuration type:

public static class UboldiApplcation {
    public static UboldiConfigurationSection Config { get; internal set; }

    public static void Initialize() {
        Config = ConfigurationManager.GetSection("uboldi") as UboldiConfigurationSection;
    }
}

The app.config then would look something like this:

<configuration>
    <configSections>
        <section name="uboldi" type="Uboldi.UboldiConfigurationSection, Uboldi" />
    </configSections>
    <uboldi schoolName="Fillmore Central" />
</configuration>

Lastly, you use the configuration by:

public void Test() {    
    //This only needs to be done once, presumably in your Program.Main method
    UboldiApplication.Initialize();
    
    var name = UboldiApplication.Config.SchoolName;
}

A couple of notes:

  1. You'll need to reference the System.Configuration assembly as it's not usually referenced in VS by default.
  2. The ConfigurationManager.GetSection("uboldi") is expecting the name of the section in the app.config file. You'll note that this matches in the example above.
  3. The section element in the app.config file uses the standard .Net type name convention to locate the specified configuration section. In this example I am assuming that the UboldiConfigurationSection type is the Uboldi namespace and in an Uboldi assembly (dll or exe).
  4. You can add hierarchy by creating ConfigurationElement sub classes and using them as properties in your configuration section and elements.
  5. The link above is for a Web.config, but the same thing is possible in an app.config file.

While somewhat of a tangent from your question but you may find it helpful nonetheless.

I would recommend looking at a custom ConfigurationSection which allows you to define more complex configuration hierarchies that are strongly-typed. I find it much easier to use a custom configuration section than having to remember a bunch of magic strings in the appSettings element and also allows you to specific which values are required, what the defaults values are, etc.

Using a custom configuration section you could create a configuration type like:

public class UboldiConfigurationSection : System.Configuration.ConfigurationSection {
    [ConfigurationProperty("schoolName")]
    public string SchoolName {
        get { return (string)this["schoolName"]; }
        set { this["schoolName"] = value; }
    }
}

Then to load that configuration type:

public static class UboldiApplcation {
    public static UboldiConfigurationSection Config { get; internal set; }

    public static void Initialize() {
        Config = ConfigurationManager.GetSection("uboldi") as UboldiConfigurationSection;
    }
}

The app.config then would look something like this:

<configuration>
    <configSections>
        <section name="uboldi" type="Uboldi.UboldiConfigurationSection, Uboldi" />
    </configSections>
    <uboldi schoolName="Fillmore Central" />
</configuration>

Lastly, you use the configuration by:

public void Test() {    
    //This only needs to be done once, presumably in your Program.Main method
    UboldiApplication.Initialize();
    
    var name = UboldiApplication.Config.SchoolName;
}

A couple of notes:

  1. You'll need to reference the System.Configuration assembly as it's not usually referenced in VS by default.
  2. The ConfigurationManager.GetSection("uboldi") is expecting the name of the section in the app.config file. You'll note that this matches in the example above.
  3. The section element in the app.config file uses the standard .Net type name convention to locate the specified configuration section. In this example I am assuming that the UboldiConfigurationSection type is the Uboldi namespace and in an Uboldi assembly (dll or exe).
  4. You can add hierarchy by creating ConfigurationElement sub classes and using them as properties in your configuration section and elements.
  5. The link above is for a Web.config, but the same thing is possible in an app.config file.
Source Link
akmad
  • 1k
  • 8
  • 7

While somewhat of a tangent from your question but you may find it helpful nonetheless.

I would recommend looking at a custom ConfigurationSection which allows you to define more complex configuration hierarchies that are strongly-typed. I find it much easier to use a custom configuration section than having to remember a bunch of magic strings in the appSettings element and also allows you to specific which values are required, what the defaults values are, etc.

Using a custom configuration section you could create a configuration type like:

public class UboldiConfigurationSection : System.Configuration.ConfigurationSection {
    [ConfigurationProperty("schoolName")]
    public string SchoolName {
        get { return (string)this["client"]; }
        set { this["client"] = value; }
    }
}

Then to load that configuration type:

public static class UboldiApplcation {
    public static UboldiConfigurationSection Config { get; internal set; }

    public static void Initialize() {
        Config = ConfigurationManager.GetSection("uboldi") as UboldiConfigurationSection;
    }
}

The app.config then would look something like this:

<configuration>
    <configSections>
        <section name="uboldi" type="Uboldi.UboldiConfigurationSection, Uboldi" />
    </configSections>
    <uboldi schoolName="Fillmore Central" />
</configuration>

Lastly, you use the configuration by:

public void Test() {    
    //This only needs to be done once, presumably in your Program.Main method
    UboldiApplication.Initialize();
    
    var name = UboldiApplication.Config.SchoolName;
}

A couple of notes:

  1. You'll need to reference the System.Configuration assembly as it's not usually referenced in VS by default.
  2. The ConfigurationManager.GetSection("uboldi") is expecting the name of the section in the app.config file. You'll note that this matches in the example above.
  3. The section element in the app.config file uses the standard .Net type name convention to locate the specified configuration section. In this example I am assuming that the UboldiConfigurationSection type is the Uboldi namespace and in an Uboldi assembly (dll or exe).
  4. You can add hierarchy by creating ConfigurationElement sub classes and using them as properties in your configuration section and elements.
  5. The link above is for a Web.config, but the same thing is possible in an app.config file.