简体   繁体   中英

Trouble accessing folder path from web config file

I was trying to use the solution found in this post: How to access the folder path in web config using c# but I get a null reference exception (System.Collections.Specialized.NameValueCollection.this[string].get returned null)?

I have this in my web config file (it's inside the configuration like shown in the post linked above):

<appSettings>
     <add key="SessionTest" value="C:\\Settings\\XmlDir\\Session\\20180824.xml"/>
</appSettings>

and retrieve it like so:

string path = System.Web.Configuration.WebConfigurationManager.AppSettings["SessionTest"].ToString();

Is there something simple that I'm missing here? The file exists in the folder (and I copied the path from the file explorer, so I'm having trouble understanding what is causing the null exception >.<")

Thanks in advance :)

据我所知,这是访问web.config <appSettings>的标准方法

System.Configuration.ConfigurationManager.AppSettings["SessionTest"]

There was a second web config file that I wasn't aware of (I'm working on an old project so I didn't realize the first team that wrote this had added one to the folder I was working in, which in hindsight is probably a little strange)

When I added it to the web config file that is associated with the entire solution and not the one inside the folder I was working in, I was able to retrieve the value successfully! I want to also note I included SLaks suggestion from the comment he made (which is not to say it doesn't work with the double slashes, but I didn't include them when I found success so I can't confirm that).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM