Ben_G Ben_G - 1 month ago 4x
C# Question

How can I reference an app.config from two separate projects (.csproj files)?

I have two .csproj files in my app that both generate an .exe and both have C# programs that need values from the app.config. So since there can only be one app.config file (they're build into the same folder) I thought I'd just put the values that the second program (let's call it b.cs/b.exe) needs in the app.config of the .csproj of the first program (a.cs/a.exe). But the second program can't seem to access the app.config. It's there and the values are there but when I run string x = ConfigurationManager.AppSettings["BuildVersion"];, x is always empty. If I run the same code from a.exe it works fine. It's as if the app.config has some info imbedded in the .exe file that I'm not able to see or set anywhere.

Any ideas how I can access values in anapp.config from two separate projects?


You can leave the app.config in one project, but add it as a linked (virtual) file in the other project. Instructions from here Including content files in .csproj that are outside the project cone

  • Right click on the project in VS.
  • Add -> Existing Item...
  • Find the file.
  • Select it and,
  • Add as a Link (drop down in the Add Button in the dialog).