setting xml file path in web.config file - c#

My XML file resides in the App_Data folder of my asp.net project. I want to set the path of my XML file in the web.config file so that it can be accessed in my class libraries. I want it because when I move my project to our university computer it makes problem. Please anybody help me write the code in web.config file and also C# code through which it can be accessed.
Also I should mention I have googled this topic and also searched on stack overflow but nothing matched my case

add this in your configuration section :
<appSettings>
<add key="xmlPath" value="C:/Users/Jonesy" />
</appSettings>
then in the code :
string path = ConfigurationManager.AppSettings["xmlPath"];
you may need to add a reference to System.Configuration.

Related

reading app settings as null from App.config

I am struggeling reading data from a configuration file, and all the methods online are not working for me...
I have this configuration file (App.config):
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="test" value="testVal"/>
</appSettings>
</configuration>
And this line in my C# code
string appSettings = ConfigurationManager.AppSettings["test"];
for some reason, appSettings remains null when expected to be testVal.
This should work. Put a break point right after that and check the value. I tested same code and getting the value.
If you use web application make sure you put this in web.config and not in the app.config
Something that may be happening is the App.config isn't being included in the build output. Check the bin folder of your project directory (Debug/Release), and ensure there's a file that matches the namespace of your project with a ".config" extension. You should see, for example:
YourApp.dll
YourApp.exe
YourApp.dll.config (edit this in notepad to verify it contains the
appsetting values)
...
Assuming this is the case and you don't see the config, or it doesn't have the correct values, ensure the App.config file in your project is included in the project itself (not just added manually to the project directory through file explorer).
If it is included and still not working, try deleting and re-creating a new "Application Configuration File" in your project in Visual Studio... that should clear things up (hopefully!). Be sure to save the original app.config contents beforehand when doing this.

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 :)
As far as I'm aware this is the standard way to access 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).

Using Directory.GetFiles in my .net website

When I built my .net site, I retrieved files paths using
Directory.GetFiles(#"D:\project\images");
But when I deployed it on internet server, I have to change the link of this folder, can you help me how can I do that?? can I use relative link in Directory.GetFiles() or how??
Put the path in your web.config. You shouldn't be hard coding paths anyway. What if it changes in the future?
In your settings add:
<appSettings>
<add key="myPath" value="D:\project\images"/>
</appSettings>
...and then call it from your application:
var myPath = WebConfigurationManager.AppSettings["myPath"];
If you really want to go nuts, look into web.config transformations so that when you publish, your release configuration will be transformed and applied for you!
in the web.config have something like:
<configuration>
<appSettings>
<add key="ImagesFolder" value="\Images" />
</appSettings>
</configuration>
then in your ASP.NET C# code behind you can use:
var ImagesFolder = ConfigurationManager.AppSettings["ImagesFolder"];
var files = Directory.GetFiles(Server.MapPath(ImagesFolder));
mind that you need to add a reference to the System.Configuration assembly or you won't be able to add the using statement and access the ConfigurationManager.
in this way there are no hard coded values and you can write the value you want for that appsetting by editing the web.config file in the deployed folder on the web server.
The correct way to do this is by using Server.MapPath
Which maps the virtual path in your web app to the physical path in the server.

Configuration from App.config isn't being pulled correctly

I'm trying to extract a URL I saved to the app.config file, but it's returning a blank string. Any ideas why?
string asdf = String.Format("{0}", ConfigurationManager.AppSettings["MemberUrl"]);
And the configuration file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="ForumUrl" value="http://www.dreamincode.net/forums/xml.php?showforum=" />
<add key="MemberUrl" value="http://www.dreamincode.net/forums/xml.php?showuser=" />
</appSettings>
</configuration>
If the app.config is part of a class library it probably isn't being copied to the bin folder properly (if at all).
The config file must be named <exefilename>.config for it to be picked up by the running application.
The App.config file in the application project (the one that produces an exe file, Console, WinForms, etc.) will copy and rename on deployment. Or if this is being executed from a web project it needs to go in the web.config.
Does this help?
All config information that your class library needs must be in the main projects App.config or web.config. In other words, if your app.config file is attached to the library it will NOT be read.
Go to the main application and add the appropriate keys/values to it's config file.
Sergio I just tried this is a console application and it works perfectly.
I would suggest that it's a class library; and not a main assembly that you have added your app.config file to.
When you do a build; look in the binary output folder Debug or Release and in there you should see a file named yourEXEfilename.config; if that file is not there then you will not get any output from the line of code you have above.
AppSettings will return a NULL string.
Hope this is of use
Kind Regards
Noel
there's no reason why that wouldn't work - do you have any other pertinent info ?
FYI, you dont need String.Format for what you're doing, the following is fine
string asdf = ConfigurationManager.AppSettings["MemberUrl"];

Windows Service Config File C#

I've developed a windows service application using Visual Studio 2008 / C#.
I have an app.config file in the project. When installed, the app.exe.config file appears beside the executable but it appears not to be reading the values from it when I try to access them through ConfigurationManager.AppSettings.
Has it copied the config file elsewhere or is there some other problem I don't know about?
Thanks in advance,
Martin.
Edit:
The config file name is infact my_exe_file_name.exe.config, it looks like:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="RuntimeFrequency" value="3" />
</appSettings>
</configuration>
and I am trying to read via:
ConfigurationManager.AppSettings["RuntimeFrequency"]
The debug value I continually see is '1' and not '3'. Am I doing something wrong here?
I located the error and it was related to file permissions. After installing the service, my local user account didn't have access to modify the app.exe.config file.
The tool I was using to edit was not informing me it was being denied access to save the file - that's notepad++ if anyone is interested - so I couldn't see that it wasn't saving over the old config file.
Solved now, thanks everyone.
Martin.
When you are in debug mode check and see what settings are in the my_exe_file_name.vshost.exe.config Also make sure you adjust this in the app.config file. Visual studio should update the final config file in your bin/debug folders.
Maybe you are updating the wrong config file. You should double check that using
System.Configuration.ConfigurationManager.OpenExeConfiguration(PATH_TO_CONFIG);
Generally for the Windows Services that I write, i drop the appName.exe.config file into C:\WINDOWS\system32\
Perhaps you have an older version in that directory, which is where your service is getting the value, even though you've updated the config file in your project.
App.config file should be renamed to your_exe_file_name.exe.config and placed near the exe file.
Is it possible that you have more than one instance of the RuntimeFrequency entry defined? The ConfigurationManager reads the file from top to bottom and processes each setting individually. Therefore, the last value of RuntimeFrequency that is defined in the file is the one it will use.
If you want to know for sure if your file is being used, I would simply remove or comment out any definition for RuntimeFrequency (copy/paste errors do happen) and wait to see an application error when ConfigurationManager attempts to reference an entry in AppSettings that does not exist.

Categories

Resources