Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to get value from a config file of other project

How to get value from a config file of other project

Scheduled Pinned Locked Moved C#
tutorial
13 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N NarVish

    Hi, In my solution, I have two projects, named as project1 and project2. project1 has app.config, contains value for the key "IndexPath". I would like to utilize this value in project2. How to read that value in project2. Thanks in advance.

    D Offline
    D Offline
    Daniel Grondal
    wrote on last edited by
    #2

    How do you intend to deploy these projects? You could open that file on your build machine, but that will most likely not work after deployment. Can you specify a bit more about you actually want to achieve?

    //daniel

    N 1 Reply Last reply
    0
    • D Daniel Grondal

      How do you intend to deploy these projects? You could open that file on your build machine, but that will most likely not work after deployment. Can you specify a bit more about you actually want to achieve?

      //daniel

      N Offline
      N Offline
      NarVish
      wrote on last edited by
      #3

      Project1 has "IndexPath" key in its config file. Project1 creates index files in "IndexPath" location. Project2 has to work with those index files, created by Project1. So in Project2, I'm trying to read "IndexPath" value specified in the cofig file of Project1. Hope my requirement is clear.. Thank you. Please let me know if I'm not clear

      D 1 Reply Last reply
      0
      • N NarVish

        Project1 has "IndexPath" key in its config file. Project1 creates index files in "IndexPath" location. Project2 has to work with those index files, created by Project1. So in Project2, I'm trying to read "IndexPath" value specified in the cofig file of Project1. Hope my requirement is clear.. Thank you. Please let me know if I'm not clear

        D Offline
        D Offline
        Daniel Grondal
        wrote on last edited by
        #4

        I am still not sure this is what you really want to do, but this might get you started:

        static void Main(string[] args)
        {
        //
        // Read your "local" App.config file
        //
        string localValue = ConfigurationManager.AppSettings["Akey"];
        Console.WriteLine("Local Value: " + localValue);

             //
             // Read a "remote", i.e. a different App.config file.
             //
             Configuration c = ConfigurationManager.OpenMappedExeConfiguration(
               new ExeConfigurationFileMap() { ExeConfigFilename = @"C:\\temp\\App.config"},
               ConfigurationUserLevel.None);
             
             string remoteValue = c.AppSettings.Settings\["AKey"\].Value;
             Console.WriteLine("Remote value: " + remoteValue);
        
             Console.ReadLine();
          }
        

        This will read your App.config file residing in c:\temp. The app.config file I used looks as:

        <?xml version="1.0" encoding="utf-8" ?>
        <configuration>
        <appSettings>
        <add key="AKey" value="YaddaYaddaYoo"/>
        </appSettings>
        </configuration>

        N 1 Reply Last reply
        0
        • N NarVish

          Hi, In my solution, I have two projects, named as project1 and project2. project1 has app.config, contains value for the key "IndexPath". I would like to utilize this value in project2. How to read that value in project2. Thanks in advance.

          B Offline
          B Offline
          BobJanova
          wrote on last edited by
          #5

          This seems like one of those things which is a bit hard because you shouldn't be doing it. I see a few scenarios where you might think you want this, and an answer to each of them.

          The two tasks are part of one overall task, which is to create the files and then work on them.

          Solution: make them both part of the same project.

          The second task uses files created in the first as input, but is not directly linked.

          Solution: put the path in the config of both projects. If the tasks can be run independently, those paths might not be the same. By reading from the config of one project you are forcing that project to be there, even though all you need is the files it created.

          The two tasks are loosely coupled (so shouldn't be one project) but expected to be run in sequence in the same location

          Solution: either have a controller project which calls one and then the other, passing the path as some sort of parameter (command line would be fine); or, store configuration information in a known common place (e.g. registry, user Local Settings folder, etc) and have both projects refer to that. (Essentially that is a controller but with no code, only data.)

          By explicitly reading from an app.config file for another application, you are requiring that that application is present in order to run yours. If Project2 has that sort of requirement on Project1, it should be a part of the same project. My guess is that it doesn't, it only depends on the files Project1 creates, and therefore you should either point it at those files in its own config, or chain the two projects from a controller which sets the location for both of them.

          realJSOPR 1 Reply Last reply
          0
          • D Daniel Grondal

            I am still not sure this is what you really want to do, but this might get you started:

            static void Main(string[] args)
            {
            //
            // Read your "local" App.config file
            //
            string localValue = ConfigurationManager.AppSettings["Akey"];
            Console.WriteLine("Local Value: " + localValue);

                 //
                 // Read a "remote", i.e. a different App.config file.
                 //
                 Configuration c = ConfigurationManager.OpenMappedExeConfiguration(
                   new ExeConfigurationFileMap() { ExeConfigFilename = @"C:\\temp\\App.config"},
                   ConfigurationUserLevel.None);
                 
                 string remoteValue = c.AppSettings.Settings\["AKey"\].Value;
                 Console.WriteLine("Remote value: " + remoteValue);
            
                 Console.ReadLine();
              }
            

            This will read your App.config file residing in c:\temp. The app.config file I used looks as:

            <?xml version="1.0" encoding="utf-8" ?>
            <configuration>
            <appSettings>
            <add key="AKey" value="YaddaYaddaYoo"/>
            </appSettings>
            </configuration>

            N Offline
            N Offline
            NarVish
            wrote on last edited by
            #6

            Thank you, its working..

            D 1 Reply Last reply
            0
            • N NarVish

              Thank you, its working..

              D Offline
              D Offline
              Daniel Grondal
              wrote on last edited by
              #7

              Ok, good. Please do think about this though. As I tried to point out (and others with me), this might not be a good solution even if it is doable. Thinking a bit ahead I do see problems wth deployment and that this creates an unattractive dependency between projects.

              //daniel

              1 Reply Last reply
              0
              • B BobJanova

                This seems like one of those things which is a bit hard because you shouldn't be doing it. I see a few scenarios where you might think you want this, and an answer to each of them.

                The two tasks are part of one overall task, which is to create the files and then work on them.

                Solution: make them both part of the same project.

                The second task uses files created in the first as input, but is not directly linked.

                Solution: put the path in the config of both projects. If the tasks can be run independently, those paths might not be the same. By reading from the config of one project you are forcing that project to be there, even though all you need is the files it created.

                The two tasks are loosely coupled (so shouldn't be one project) but expected to be run in sequence in the same location

                Solution: either have a controller project which calls one and then the other, passing the path as some sort of parameter (command line would be fine); or, store configuration information in a known common place (e.g. registry, user Local Settings folder, etc) and have both projects refer to that. (Essentially that is a controller but with no code, only data.)

                By explicitly reading from an app.config file for another application, you are requiring that that application is present in order to run yours. If Project2 has that sort of requirement on Project1, it should be a part of the same project. My guess is that it doesn't, it only depends on the files Project1 creates, and therefore you should either point it at those files in its own config, or chain the two projects from a controller which sets the location for both of them.

                realJSOPR Online
                realJSOPR Online
                realJSOP
                wrote on last edited by
                #8

                BobJanova wrote:

                This seems like one of those things which is a bit hard because you shouldn't be doing it.

                I have to disagree. There have been a number of times where I wanted to read a config file from another app. All he has to do is load it as a standard xml file, and manually read the data. That's not hard at all.

                ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                -----
                You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                -----
                "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997

                B 1 Reply Last reply
                0
                • N NarVish

                  Hi, In my solution, I have two projects, named as project1 and project2. project1 has app.config, contains value for the key "IndexPath". I would like to utilize this value in project2. How to read that value in project2. Thanks in advance.

                  realJSOPR Online
                  realJSOPR Online
                  realJSOP
                  wrote on last edited by
                  #9

                  It's just XML. Load the file and read the settings. It's as easy as that.

                  ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                  -----
                  You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                  -----
                  "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997

                  1 Reply Last reply
                  0
                  • N NarVish

                    Hi, In my solution, I have two projects, named as project1 and project2. project1 has app.config, contains value for the key "IndexPath". I would like to utilize this value in project2. How to read that value in project2. Thanks in advance.

                    P Offline
                    P Offline
                    PIEBALDconsult
                    wrote on last edited by
                    #10

                    That's part of why I don't use app.config. I roll my own and I can have applications sharing common config files.

                    1 Reply Last reply
                    0
                    • realJSOPR realJSOP

                      BobJanova wrote:

                      This seems like one of those things which is a bit hard because you shouldn't be doing it.

                      I have to disagree. There have been a number of times where I wanted to read a config file from another app. All he has to do is load it as a standard xml file, and manually read the data. That's not hard at all.

                      ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                      -----
                      You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                      -----
                      "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997

                      B Offline
                      B Offline
                      BobJanova
                      wrote on last edited by
                      #11

                      That's true, going in at the XML level is not hard, as long as MS keep the file format the same. Is the format of those config files a published standard that can be relied on?

                      realJSOPR 1 Reply Last reply
                      0
                      • B BobJanova

                        That's true, going in at the XML level is not hard, as long as MS keep the file format the same. Is the format of those config files a published standard that can be relied on?

                        realJSOPR Online
                        realJSOPR Online
                        realJSOP
                        wrote on last edited by
                        #12

                        Nothing that comes out of Redmond can be relied upon. If it's their standard, they can change it whenever their current CEO farts if they want to.

                        ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                        -----
                        You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                        -----
                        "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997

                        L 1 Reply Last reply
                        0
                        • realJSOPR realJSOP

                          Nothing that comes out of Redmond can be relied upon. If it's their standard, they can change it whenever their current CEO farts if they want to.

                          ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                          -----
                          You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                          -----
                          "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #13

                          I think Ballmer just farted :laugh: Yes there is a link to this in the lounge... But seriously, do you smell that??? What did that man eat!?!?[^]

                          Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet.

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups