Help accessing application settings using ConfigurationManager - c#

In .net frameworks 1.1, I use
System.Configuration.ConfigurationSettings.AppSettings["name"];
for application settings. But in .Net 2.0, it says ConfigurationSettings is obsolete and to use ConfigurationManager instead. So I swapped it out with this:
System.Configuration.ConfigurationManager.AppSettings["name"];
The problem is, ConfigurationManager was not found in the System.Configuration namespace. I've been banging my head against the wall trying to figure out what I'm doing wrong. Anybody got any ideas?

You have to reference the System.configuration assembly (note the lowercase)
I don't know why this assembly is not added by default to new projects on Visual Studio, but I find myself having the same problem every time I start a new project. I always forget to add the reference.

If you're just trying to get a value from the app.config file, you might want to use:
ConfigurationSettings.AppSettings["name"];
That works for me, anyways.
/Jonas

You are missing the reference to System.Configuration.

Visual Studio doesn't make it obvious which assembly reference you need to add. One way to find out would be to look up ConfigurationManager in the MSDN Library. At the top of the "about ConfigurationManager class" page it tells you which assembly and DLL the class is in.

System.Configuration us refer to System.configuration (not the small case for configuration, in .net 2.o it reefers to System.Configuration.dll.

Related

What is The Nuget Package For System.Web.Hosting

I have been researching but I can't seem to find any relevant answer. I will appreciate it if someone told me the nuget package to install in asp.net webapi application for System.Web.Hosting
I need it to use Hosting.HostingEnvironment.MapPath("")
Add a reference to System.Web assembly, you will then be able to access System.Web.Hosting.HostingEnvironment
Since this is in the System namespace, its very likely part of the Framework.
Googling for that namespace brings it up on MSDN. So, open up the documentation for that namespace on MSDN. Click on any class in the list, it doesn't matter which.
You will see something like this:
Namespace: System.Web.Hosting
Assembly: System.Web (in System.Web.dll)
You now know which assembly this particular class is in, which likely also contains most of the rest of the desired namespace. Check another class on MSDN to confirm. Well, this is System.Web. That's part of the Framework.
If you do this for another namespace and can't find it on MSDN, you're probably looking for a Nuget package. But in this case, nope. Just add a reference to the assembly.
System.Web is not a nugget package.
Go to Solution explorer > References > Add References > Framework > look for System.Web.
System.Web.Hosting.HostingEnvironment.MapPath(string.Empty);
For .Net Core use
Microsoft.AspNetCore.Hosting

CS0618 'ConfigurationSettings.GetConfig(string)' is obsolete [duplicate]

I got the following warning
'System.Configuration.ConfigurationSettings.AppSettings' is obsolete:
'"This method is obsolete, it has been replaced by
System.Configuration!System.Configuration.ConfigurationManager.AppSettings"'
How do you fix it?
Add a reference to the assembly System.Configuration.
Then at the top (assuming C#) using System.Configuration (Imports System.Configuration in VB.NET).
Use ConfigurationManager.AppSettings["MySetting"] to access the settings!
as its a warning i dont think it matters unless you have turned off a treat warnings as errors setting
add a reference to System.Configuration
all you have to do is to update to the latest code so where you used ConfigurationSettings.AppSettings[""] change to ConfigurationManager.AppSettings[""]
and this should work
Just in case someone else was looking for the Add Reference option to achieve the accepted answer in Visual Studio 2010. (I had to do this in updating a VB project).
In Visual Studio 2010:
Click on Project > Add Reference.
Click on the C# tab (even though mine was a pure VB project)
Scroll down halfway to find System.Configuration (I had v4 since .NET Framework 4.0 was the chosen version)
Click OK, then update the line of code as per the suggestion given.
From System.Configuration.ConfigurationSettings.AppSettings("name")
to System.Configuration.ConfigurationManager.AppSettings
Without adding the reference IntelliSense won't suggest ConfigurationManager when you type it, and that's because it doesn't have a reference to be aware of where it is. Which is also why you will get errors when you updated the line of code according to their suggestion.
the System.configuration DLL
exsit in c:\Windows\Microsoft.NET\Framework\v2.0.50727\
After adding the reference using System.Configuration; at the top of the class. Still the same warning remains.
In Code Behind:
Instead of ConfigurationSettings.AppSettings["ConnectionString"]
Use ConfigurationManager.AppSettings["ConnectionString"]
By Default the System.configuration Dll will be added in your project.
In Web.config or App.config:
<add key="ConnectionString" value="Some Connection Strings or Specific Path"/>
to use ConfigurationManager.AppSettings[""] Add Reference Assemblies not use using System.Configuration;
Just replace
System.Configuration.ConfigurationSettings.AppSettings
with
System.Configuration!System.Configuration.ConfigurationManager.AppSettings
in your code.
you must add reference of System.onfiguration in your project then add "Using System.onfiguration;"
next step using like this:
private string SQLConnectionString = ConfigurationManager.AppSettings["SQlConnectionString"];
example:
replace
string smtpServer = System.Configuration.ConfigurationSettings.AppSettings["EmailServer"];
with
string smtpServer = ConfigurationManager.AppSettings["EmailServer"];
also make sure on the top of the case you add:
using System.Configuration;
It's simple as mentioned above, just add a reference "System.Configuration" for the application, and within the code you can add "using System.Configuration" to the top of the code and use "ConfigurationManager.AppSettings[""]" where you need it.
I also face same issue, sometimes the assembly reference not loaded properly or if you are using multiple projects it give problems sometimes. You just add reference to assembly. Right Click>Add Reference>.Net>System.configuration> Click OK
You can see now there are many configuration options available choose ConfigurationManager.AppSetting["Con"].ToString();
Build and Smile :)
I had the same problem in a C# project and I fixed it by writing appSettings instead of AppSettings in the XML file (camelCase expected) in the tag
<appSettings>
<add key="myKey" value="my Value"/>
<appSettings>
After all C# is case sensitive

How can I use the objects ConnectionStringSettings and ConfigurationManager into my C# - NUNIT project?

I'm trying to apply the solution proposed in this question: Using the connectionstring in an nunit test but I can't seem to find the ConnectionStringSettings and ConfigurationManager objects in the new Framework 4.5.
I added the namespace: System.Configuration. Where acording Microsoft Documentation is supposed to be. But Visual Studio still cant find it.
Could anyone here give me a hand with this?
Add reference to System.Configuration to your project..
Add reference to System.configuration to your project and use that dll in files

Why can't I find or use UrlEncode in Visual Studio 2010?

I have a string that I'd like to encode into the standard URL format. From what I've found, I should be able to do this via the httpUtility.urlEncode method, but I don't seem to have that available.
I've added "using" references to both System.Web and System.Net to no avail. I've also seen other references to server.urlEncode amongst other variants, but I don't see the method anywhere.
I'm using the latest version of C# in Visual Studio 2010. Is the method called something different in this version, hidden somewhere else, or am I completely off base?
By default, new projects in Visual Studio 2010 target the .NET Framework 4.0 Client Profile, which does not include the System.Web assembly.
You can change the version of the Framework that your project targets in your project's Properties. Under the "Application" tab, select ".NET Framework 4.0" from the combobox labeled "Target framework".
Then, make sure that you have added a reference to System.Web using the "Add Reference" dialog.
Finally, add a using directive to the top of your class for the System.Web namespace:
using System.Web;
You'll find the various overloads of the UrlEncode method in the HttpUtility class. Sample code:
HttpUtility.UrlEncode("http://www.google.com/");
In .Net 4.5 you can (should?, 'please use' says a Katana comment) use the System.Net.WebUtility.UrlEncode method.
It can't be named differently since Visual Studio doesn't supply the class or method names, the .NET framework does.
All I can tell you is that the System.Web.HttpUtility AND System.Web.HttpServerUtility classes contain a method called UrlEncode(string).
If your project target ".NET Framework X Client Profile",you cannot not use "System.Web",but you can use "Uri.EscapeUriString | Uri.UnEscapeUriString" instead.
Yes, adding the reference was my answer. But be sure you double check the project, that it is in, if you have more than 1 project in your solution. I had a solution with 3 projects. System.Web was added to 2 projects but not the 3rd project.
I spent an hour trying to figure out why I couldn't use HttpUtility since it was a Reference in the main project. But I didn't check the sub-projects of the Solution.
Hope it helps someone.
Because you only see AspNetHostingPermission, AspNetHostingPermissionAttribute, and AspNetHostingPermissionLevel, I strongly suspect (like the other guys) that you're missing a reference.
The best you can do is start a new project, because it's pretty complicated to add/remove references without ruining your entire project.
How to: Add or Remove References in Visual Studio (MSDN) shows how to add/remove references. In your case, you should check/add the System.Web reference.

Can't find HttpContext in .NET 3.5 and Visual Studio 2008

I used code from here and I get the following error:
Can't use HttpContext.Current.Server.MapPath()
In Visual Studio 2008 does the ContextMenuEntry "Solve" help you when you have missing references?
I already found out that HttpContext is not a member of System.Web in my IDE.
According to Help > Info I am using .NET 3.5 SP1.
How do I get that running?
How do u usually react in this situation? What stuff do u look for in msdn.com?
What I would do in that situation is look on MSDN (or Google) for HttpContext. I did that, and it says it’s in System.Web. So make sure your project has a reference to System.Web.
... and then it seems to work:
You can look in the documentation for the HttpContext class, and it tells you that it's in the System.Web namespace, in the System.Web.dll library.
So, to use it you need a reference to the System.Web.dll library, and you either need a using System.Web; statement, or use the fullly qualified name System.Web.HttpContext.Current.Server.MapPath.
However, are you sure that you want to use the MapPath method? The method gets the physical path of a web reference to a file. If the path to your CSV file is a web reference, for example "/data/items.csv" then you want to use the MapPath method, but if you have a physical path like for example "C:\mydata\items.csv" then you don't want to convert it.
Also, the MapPath only works if you actually are in a web application, where there is a HTTP context.
Timwi has it right, but for completeness. No, VS does not have the 'Solve' capability built in, however this functionality has been partially added by some add-ons. For example, Resharper will add the option to add the reference and using when needed -- but it does have to have been referenced before in the solution so it doesn't solve the initial find problem.
It was a simple case of not using the right framework, by that I mean the full fat version rather than the default 'light' version.
Right click on the Project and then Properties and make sure the full version of the latest framework is selected ie '.NET Framework 4', not '.NET Framework 4 Client Profile'
Try to add a reference to System.Web in your project.
HttpContext is a member of System.Web.
http://msdn.microsoft.com/en-us/library/system.web.httpcontext.aspx

Categories

Resources