How to get value from a config file of other project
-
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.
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
-
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
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
-
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
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> -
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.
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.
-
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> -
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
-
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.
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 -
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.
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 -
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.
That's part of why I don't use app.config. I roll my own and I can have applications sharing common config files.
-
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 -
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?
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 -
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, 1997I 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.