Surely it's not the case that I have to create a reader and read the lines as an XML file and hunt for what I want. With VS2005, using the automated tools, it's putting my settings someplace other than appSettings, but I can't seem to find anything in C#
that will read the settings. I keep finding things that are types and tell me that if found my setting, but since it's a type, it won't read the string that it is defined as.
I tried that, but it didn't work. I certainly did back in VS 2003, but we have to move on, I suppose.
However, in a sample project for something else, I found a config file with this new type of applicationSettings rather than the simple appSettings, and I found the way he was reading them to be quite simple, though I doubt I would have ever guessed it.
pathToScripts = MoHuntEdWebApp.Properties.
Settings.Default.PathToCmdFiles;
So, instead of using the string "PathToCmdFiles" as the index into a collection, in C# this is apparently now a member of the default class. I guess that's what "Default" means in this context. I saw the choice in intellisense, if just
didn't occur to me that default would be useful for finding out my current settings. The word seemed to imply generic stuff of some kind. But if you put it in, then intellisense will display all your settings names.
My password must be written down, let's make sure it is secure. But of course, I misplaced the paper, so I have to keep asking for a new one.
This only works if in fact the system has put your settings in <appSettings />. However, in VS2005, if you use the IDE (i.e. the application or website properties) to create and manage your key-value pairs, it puts them in a different place. This returns
a null in my situation. And while I could change it around, now the problem will be that the network group will have to edit the config files manually instead of being able to use the IIS properties for the website.
My password must be written down, let's make sure it is secure. But of course, I misplaced the paper, so I have to keep asking for a new one.
VS2005 no longer creates "appSettings" entries, instead there is now a two stage entry, one part that defines what sections are present in the file, and the other part those sections. Where can I find out how to read information from these new sections that
will work for both web applications and web services?
But I had to move the config information to a web service, and this doesn't work any more. (Yes, I changed the first name to the name of the webservice).
It says my MoHuntEdWebSvc does not have ".Properties". However I set up the web config by using the properities item in the solution explorer. The result is entries in the web.config file that are the same for the app as the web service:
Here is the type of stuff found in both config files
Up at the top there is this business, which is surely what the IDE and IIS use when presenting a graphic wrapper around editing the application Properties. I can see where the "ApplicationName.Properties.Settings.Default.NameOfKey"
can be used to find stuff. But when I try to write this in C# it keeps telling me that the only right way to do it is what was suggested earlier, "ConfigurationManager.AppSettings["VersionOfWebSite"];"
But that only works with the old style web.config structure that has an
appSettings section in the webconfig.
How am I supposed to know I need to prepend "global::"?
I found it by clicking on find definition over in the other app where the Properties of the app exist. That sent me to a generated file, reference.cs, where I saw this "global::" thing.
My password must be written down, let's make sure it is secure. But of course, I misplaced the paper, so I have to keep asking for a new one.
JustWantAnAn...
Member
78 Points
164 Posts
How to read application settings from web.config?
Aug 04, 2008 06:09 PM|LINK
Surely it's not the case that I have to create a reader and read the lines as an XML file and hunt for what I want. With VS2005, using the automated tools, it's putting my settings someplace other than appSettings, but I can't seem to find anything in C# that will read the settings. I keep finding things that are types and tell me that if found my setting, but since it's a type, it won't read the string that it is defined as.
This is what shows up in the config file:
<applicationSettings><
MoHuntEdWebApp.Properties.Settings> <setting name="PathToCmdFiles" serializeAs="String"><
value>~\Applications\MoHuntEdWebApp\scriptfiles\</value></
setting> </MoHuntEdWebApp.Properties.Settings></
applicationSettings>MSDN says to do this:
System.Configuration.Configuration rootWebConfig1 =System.Web.Configuration.
WebConfigurationManager.OpenWebConfiguration(null);and
pathToIntranetArc = rootWebConfig1.AppSettings.Settings[
"PathToIntranetArc"].Value ;but when i run it, it says rootWebConfig1.AppSettings.Settings["PathToIntranetArc"] is null
Isn't there a settings object created when the page is loaded?
C# 2005 Generics my.settings settings web.config applicationSettings appSettings
budugu
All-Star
41132 Points
6021 Posts
Re: How to read application settings from web.config?
Aug 04, 2008 06:26 PM|LINK
I guess , you have to add it to appSettings, Something like this..
"Don't be afraid to be wrong; otherwise you'll never be right."
JustWantAnAn...
Member
78 Points
164 Posts
Re: How to read application settings from web.config?
Aug 04, 2008 06:51 PM|LINK
I tried that, but it didn't work. I certainly did back in VS 2003, but we have to move on, I suppose.
However, in a sample project for something else, I found a config file with this new type of applicationSettings rather than the simple appSettings, and I found the way he was reading them to be quite simple, though I doubt I would have ever guessed it.
pathToScripts = MoHuntEdWebApp.Properties.
Settings.Default.PathToCmdFiles;So, instead of using the string "PathToCmdFiles" as the index into a collection, in C# this is apparently now a member of the default class. I guess that's what "Default" means in this context. I saw the choice in intellisense, if just didn't occur to me that default would be useful for finding out my current settings. The word seemed to imply generic stuff of some kind. But if you put it in, then intellisense will display all your settings names.
SGWellens
All-Star
126031 Points
10310 Posts
Moderator
Re: How to read application settings from web.config?
Aug 04, 2008 09:50 PM|LINK
Here is some sample code:
// Reading a key String Version = ConfigurationManager.AppSettings["VersionOfWebSite"]; // Writing a key ExeConfigurationFileMap FileMap = new ExeConfigurationFileMap(); FileMap.ExeConfigFilename = Server.MapPath(@"~\Web.config"); Configuration Config = ConfigurationManager.OpenMappedExeConfiguration(FileMap, ConfigurationUserLevel.None); Config.AppSettings.Settings.Add("Test", "TestValue"); Config.Save(ConfigurationSaveMode.Modified);My blog
JustWantAnAn...
Member
78 Points
164 Posts
Re: How to read application settings from web.config?
Aug 05, 2008 01:40 PM|LINK
This only works if in fact the system has put your settings in <appSettings />. However, in VS2005, if you use the IDE (i.e. the application or website properties) to create and manage your key-value pairs, it puts them in a different place. This returns a null in my situation. And while I could change it around, now the problem will be that the network group will have to edit the config files manually instead of being able to use the IIS properties for the website.
JustWantAnAn...
Member
78 Points
164 Posts
Re: How to read application settings from web.config?
Aug 11, 2008 04:20 PM|LINK
VS2005 no longer creates "appSettings" entries, instead there is now a two stage entry, one part that defines what sections are present in the file, and the other part those sections. Where can I find out how to read information from these new sections that will work for both web applications and web services?
I had marked myself as answered by using
pathToScripts = MoHuntEdWebApp.Properties.Settings.Default.PathToCmdFiles;
But I had to move the config information to a web service, and this doesn't work any more. (Yes, I changed the first name to the name of the webservice).
It says my MoHuntEdWebSvc does not have ".Properties". However I set up the web config by using the properities item in the solution explorer. The result is entries in the web.config file that are the same for the app as the web service:
Here is the type of stuff found in both config files
Up at the top there is this business, which is surely what the IDE and IIS use when presenting a graphic wrapper around editing the application Properties. I can see where the "ApplicationName.Properties.Settings.Default.NameOfKey" can be used to find stuff. But when I try to write this in C# it keeps telling me that the only right way to do it is what was suggested earlier, "ConfigurationManager.AppSettings["VersionOfWebSite"];" But that only works with the old style web.config structure that has an appSettings section in the webconfig.
<configSections><
sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" > <section name="MoHuntEdWebApp.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /></
sectionGroup> </configSections> <applicationSettings> <MoHuntEdWebApp.Properties.Settings><
setting name="keyusedinapplicationToRetievethis" serializeAs="String"><
value>http://localhost/MoHuntEdWebSvc/Service1.asmx</value> </setting></
MoHuntEdWebApp.Properties.Settings> </applicationSettings> And this is what the web service web.config looks like: <applicationSettings><
MoHuntEdWebSvc.Properties.Settings> <setting name="OraConnection" serializeAs="String"><
value>Provider = xxxxxxx; Data Source=yyyyyyt;User ID=aaaaa;Password=bbbbb</value></
setting> <setting name="PathToIntranetArc" serializeAs="String"><
value>~\Arc\</value></
setting> </MoHuntEdWebSvc.Properties.Settings></
applicationSettings>JustWantAnAn...
Member
78 Points
164 Posts
Re: How to read application settings from web.config?
Aug 11, 2008 07:09 PM|LINK
global
::MoHuntEdWebSvc.Properties.Settings.Default.OraConnection;How am I supposed to know I need to prepend "global::"?
I found it by clicking on find definition over in the other app where the Properties of the app exist. That sent me to a generated file, reference.cs, where I saw this "global::" thing.