Use a Name/value in Machine.config file in the web.config file

Last post 03-27-2008 12:27 PM by kckriegs. 1 replies.

Sort Posts:

  • Use a Name/value in Machine.config file in the web.config file

    03-27-2008, 12:08 PM
    • Member
      49 point Member
    • mark836
    • Member since 12-11-2007, 2:32 PM
    • Posts 167

    Hello.

    I want/need to access a variable in the machine.config file and use it in the web.config file.

    My scenario is:

    Each machine has a machine name, so (I assume this is the best place to put it) I want to create a variable in <appsettings> in the machine.config file called "MachineName".  In my web.config file, I want to use the MachineName variable in the machine.config in the appsettings.

     

    Example: (web.config file)

     ...

    <appSettings>

    <add key="ServerFileNames" value=\\ISCO1FEGRD\Dotnet_files\OrclIntlList.xml />

    Replace the "ISCO1FEGRD" with the variable "MachineName" from the Machine.config file.

     

    Thanks

     

  • Re: Use a Name/value in Machine.config file in the web.config file

    03-27-2008, 12:27 PM
    Answer
    • Contributor
      2,024 point Contributor
    • kckriegs
    • Member since 08-15-2005, 3:55 PM
    • Posts 435

    Keys in web.config are not populated dynamically. I.E., you can't set a "macro" in web.config that pulls values from machine.config. But you shouldn't have too...

    In your C# or VB.NET code, you pull values from machine.config exactly the same way you pull from web.config. ASP.NET knows to check in machine.config as well as web.config; and there are rules for which value overrides.

    So, in your code, just stitch together your "ServerFileNames" path by making an appsettings lookup for "MachineName", and concatenating your "ServerFileNames" folder path.

    Machine.config:

    <add key="MachineName" value="ISCO1FEGRD"

    Web.config:

    <add key="ServerFileNames" value="\Dotnet_files\OrclIntlList.xml" />

     

Page 1 of 1 (2 items)