5
var userName = ConfigurationManager.AppSettings["FileUploadRoot"];

Iam getting null for the variable Username. The key FileUploadRoot is in another projects web config entry and the above piece of code is in another projects class file. so can you please tell me how to get the value from the web config file

Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
VVN
  • 501
  • 1
  • 9
  • 21
  • This piece of code is going to read `AppSettings` from the project where it is written not from another project. And within your project the key "FileUploadRoot" doesn't exist. – M. Adeel Khalid Feb 28 '17 at 07:29
  • yes i do understand. so can you please tell me how to read it from another projects web config – VVN Feb 28 '17 at 07:35
  • [Read this](http://stackoverflow.com/questions/22785541/accessing-another-projects-app-config-properties) – M. Adeel Khalid Feb 28 '17 at 07:36

2 Answers2

3

The best way would be to include the config value in your first project too, to make it self-contained.

You can use a static class in the project that has the web.config file and expose the value in the web.config in an internal property

You can use something like this:

  static class Utility
    {
        internal static string FileUploadRoot
        {
            get
            {
                return ConfigurationManager.AppSettings["FileUploadRoot"];
            }
        }
    }

Another solution would be to add the file as a link to your project.

  1. Right click on you project in solution explorer.
  2. Select Add Existing Item...
  3. Select the web.config file in the OpenFileDialog, but choose the Add as Link option in the dialog.

enter image description here

S.Dav
  • 2,436
  • 16
  • 22
1

There is built in functionality for this:

string otherExe = @"C:\projects\otherExe\bin\Debug\BillsAndStatementsArchiver.exe";
Configuration otherConfig = ConfigurationManager.OpenExeConfiguration(otherExe);
string fileUploadRoot = otherConfig.AppSettings.Settings["fileUploadRoot"].Value;
James Cooke
  • 1,086
  • 8
  • 7