2

Currently I have 2 apps, App1 and App2.

App1 has an app.config file where I store the string InstallDir. I can access this from App1 fine using Properties.Settings.Default.InstallDir. Which works great, but I need to be able to access this from App2.

I changed the accessibility from Internal to Public in VS. When I try to access it from App2 I add a reference to App1 and use using App1; and App1.Properties.Settings.Default.InstallDir but it justs returns nothing. It doesn't throw an exception, it just returns a blank string.

Any ideas greatly appreciated.

Bali C
  • 30,582
  • 35
  • 123
  • 152
  • As a comment, why not put this information in a third place both apps can access like a separate config file or a database? The kind of config you are dicussing feels more volatile than what usually goes in app.config. – dash Mar 01 '12 at 11:42

4 Answers4

3

Every application has its own app.config file where it reads configuration from at runtime. You have referenced App1 in App2 which allows you to access the App1.Properties.Settings.Default.InstallDir variable but at runtime since there's no corresponding value in the app.config of App2 it returns null.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
2

If you want to access some (I presume) shared configuration options between 2 projects, create wrapper for the config on the first one (let's say a static class) and use it from your second project.

Tigran
  • 61,654
  • 8
  • 86
  • 123
  • Thanks. I need to retain the value of the InstallDir after the app is closed which is why I use the config file. Unless the variable in the static class is somehow retained after the app has closed? – Bali C Mar 01 '12 at 10:41
  • @Bali: how that APP can be "closed" ? Do you refer an EXE project? – Tigran Mar 01 '12 at 10:50
  • Yeah, I refer to the exe project, so when the application is closed, I need to save the value, like I can save it to the config file, so when it opens I can access it again. – Bali C Mar 01 '12 at 11:09
  • Yes but that is a duty of that application that closing (to save its Config data). You can access that information in other project still after closing. – Tigran Mar 01 '12 at 12:02
0

You can only write your own code to switch reading another config file, even you add App2 as a reference it would not magically give you access, read it as a normal file or use ConfigurationManager.OpenMappedExeConfiguration: http://msdn.microsoft.com/en-us/library/ms134269.aspx

Simon Wang
  • 2,843
  • 1
  • 16
  • 32
0

Your application will only have loaded one configuration file, if you want to read values from the other file you will need to explicitly load it.

look at Using ConfigurationManager to load config from an arbitrary location for some help with this.

Community
  • 1
  • 1
dice
  • 2,820
  • 1
  • 23
  • 34
  • Currently my app.config is compiled with the exe, so it isn't compiled as a separate file, I presume I wouldn't be able to use this until it is a separate file? – Bali C Mar 01 '12 at 10:47