zoukankan      html  css  js  c++  java
  • Using App.Config for user defined runtime parameters

    September 27, 2007

    These are the steps involved in defining runtime parameters and using them in your code.

    1. Create a App.Config file
    2. Define the runtime parameters and provide values for these
    3. Access these parameters in your code
    4. Verify changing the parameter value at runtime affects the application


    Step 1: Create App.Config file

    1a. Create a Console Application
    1b. In Solution Explorer, right click on project. Add->New Item. Select Application Configuration file. This file will be added to your project.


    Step 2: Define the runtime parameters and provide values for these

    2a. Open App.Config.
    2b. Define the keys and values for your runtime parameters.

    Example:

    <configuration>
    <appSettings>
    <add key="OperatorName" value="Rita"></add>
    <add key="LoggerLevel" value="5"></add>
    </appSettings>
    </configuration>

    You can define any key and provide values. Ensure you don’t have duplicate keys.


    Step 3: Access these parameters in your code

    3a. Add a refernce System.Configuration DLL.1
    3b. Add the following lines in your code. You can directly add these lines in Main.

    string name = System.Configuration.ConfigurationManager.AppSettings["OperatorName"];
    Console.Writeline("Welcome " + name);
    string level = System.Configuration.ConfigurationManager.AppSettings["LoggerLevel"];
    Console.Writeline("Logger level: " + level);

    3c. Build your project and execute it.The output should be:

    Welcome Rita
    Logger level: 5


    Step 4: Verify changing the parameter value at runtime affects the application

    4a. When you build your project, the App.Config file is copied to your Debug/Release folder with the name .exe.config. My project is named UseAppConfig. The App.Config in my Debug folder is UseAppConfig.exe.config

    Open the .config file in your working directory (Debug/Release). Change the values to Mary and 1. In the same folder you will see your executbale. Double click on this and you should see the new values.2

    You are all set. You can identify all those parameters that make sense as runtime parameters, define a meaningful key for each parameter and start using it. You no longer have to build your application every time you want to change the parameter value.


    Notes

    1: You need this reference only if you are using .NET  2.0
    2: If you change the App.Config from Visual Studio IDE, then you need to rebuild your project to see the new values.

  • 相关阅读:
    php自定义函数call_user_func和call_user_func_array详解
    微信开发(一) 服务器配置
    6487. 【GDOI2020模拟02.29】列强争霸war
    关于循环顺序对时间影响的一点研究
    6486. 【GDOI2020模拟02.25】向日葵人生
    6485. 【GDOI2020模拟02.25】沙塔斯月光
    6478. 【GDOI2020模拟02.19】C(上下界费用流、费用流消负环)
    6461. 【GDOI2020模拟02.05】生成树(矩阵树及其扩展、二维拉格朗日插值)
    上下界网络流&费用流
    6467. 【GDOI2020模拟02.09】西行寺无余涅槃(FWT的性质)
  • 原文地址:https://www.cnblogs.com/danielWise/p/2065493.html
Copyright © 2011-2022 走看看