2

I have JSON file stored in my D drive. I have mentioned the same in web config file. Please refer the below code

    <appSettings>
    <add key="inputFilePath" value="D:\products.json"/>
    </appSettings> 

When I try to get the file path using the below code, I am getting null value.

    string filepath = ConfigurationManager.AppSettings["inputFilePath"];

Please can any one help me on this

anitha lm
  • 61
  • 5

3 Answers3

0

you need to call like this

string filepath = ConfigurationManager.AppSettings["inputFilePath"].ToString();
Krsna Kishore
  • 8,233
  • 4
  • 32
  • 48
0

Refer this - How to: Read Application Settings from the Web.config File

System.Configuration.Configuration rootWebConfig1 =
                System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(null);
            if (rootWebConfig1.AppSettings.Settings.Count > 0)
            {
                System.Configuration.KeyValueConfigurationElement customSetting = 
                    rootWebConfig1.AppSettings.Settings["customsetting1"];
                if (customSetting != null)
                    Console.WriteLine("customsetting1 application string = \"{0}\"", 
                        customSetting.Value);
                else
                    Console.WriteLine("No customsetting1 application string");
            }

//Config file

<appSettings>
  <add key="customsetting1" value="Some text here"/>
</appSettings>

You'll need to add a reference to System.Configuration in your project's references folder.

Add namespace

using System.Configuration;

then get file path

String path = ConfigurationManager.AppSettings["inputFilePath"];

More References:
AppSettings
How to read appSettings section in the web.config file?
Getting configuration settings from web.config/app.config using class library
Reading settings from app.config or web.config in .net

Hope this help.

Community
  • 1
  • 1
Niranjan Singh
  • 18,017
  • 2
  • 42
  • 75
0

There's nothing wrong with your code. This suggests that the <appSettings> isn't in the executing project. For example, if you have a web project and some other class library and the class library refers to <appSettings>, that setting needs to be in the website's web.config. If your class library has its own app.config it's not going to be read. Whatever is executing - website, web forms, console app, etc, - that's where the <appSettings> needs to be, regardless of what is trying to read it.

Scott Hannen
  • 27,588
  • 3
  • 45
  • 62