 |
|
 |
Lightweight and working - thanks! I wish there was a section to get the mailSettings, but this should be easy to add. Just wondering whether the class will work with encrypted settings? I am still a newbie in the security area and my reason for including such a console utility is to encrypt/decrypt settings on the deployment server. Any hints?
|
|
|
|
 |
|
 |
Thanks!
It is one thing to know what to want, second to really want it, third to know how to do it, fourth to be skillful to do it, fifth to actually do it and last but not least to not regret after doing it
|
|
|
|
 |
|
|
 |
|
 |
The method SelectSingleNode does not work for me.
When I debug my console application, the xmldoc loads OK but SelectSingleNode finds nothing even when the tag shows in the innerXML property of xmldoc.
string XPath = @"/configuration/appSettings/add[@key='"+SearchKey+"']";
XmlNode node = xmldoc.SelectSingleNode(XPath);
|
|
|
|
 |
|
 |
Same problem here. This is because the configuration node has a namespace. I fixed it with the addition of a namespace manager. Just change the bold part with your own namespace and it will work.
private string SearchXmlForKey(string SearchKey)
{
string AttributeValue = "";
string XPath = @"/n:configuration/n:appSettings/n:add[@key='" + SearchKey + "']";
XmlNamespaceManager xnm = new XmlNamespaceManager(xmldoc.NameTable);
xnm.AddNamespace("n", "http://schemas.microsoft.com/.NetConfiguration/v2.0");
XmlNode node = node = xmldoc.SelectSingleNode(XPath, xnm);
if (node != null && node.Attributes["value"] != null)
{
AttributeValue = node.Attributes["value"].Value;
}
else
{
throw new Exception("Cannot find the " + SearchKey + " key");
}
return AttributeValue;
}
|
|
|
|
 |
|
 |
Thanks for the sample, it worked great.
I had to make 1 change; I have to read the file multiple times but I would get a 'file in use by another process' error.
So I simply closed the file at the end of the ConfigurationSettings function (see below).
Just an FYI for anyone else in the same boat.
Cheers
public ConfigurationSettings(string filename)
{
if (File.Exists(filename))
{
this.file = new FileStream(filename,System.IO.FileMode.Open);
XmlDocument xmldoc = this.ConvertConfigToXml(this.file);
this.settings = AppSetting.InstantiateSingletonInstance(xmldoc);
this.file.Close();
}
else
{
throw new Exception("The file could not be located");
}
}
|
|
|
|
 |
|
 |
I get
An unhandled exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll
Additional information: Access to the path "c:\advantage\Web.Config" is denied.
Which makes sense since you are not supposed to be able to remotely access web.config
Any Comments?
Doug
Don't spend your life just wishing - http://www.AllGiftRegistry.com
|
|
|
|
 |
|
 |
Just modify the ConfigurationSetting class constructor like that:
this.file = new FileStream(filename,System.IO.FileMode.Open, System.IO.FileAccess.Read);
I think the reason is that you can't modify the Web.Config, you can just read it.
|
|
|
|
 |
|
|
 |
|
 |
No, this class is for use in standalone windows and console applications to allow access to web.config; unlike the built in .net class which is for use within web based applications.
|
|
|
|
 |
|
|
 |
|
 |
Uwe,
First of all lets try and keep this discussion at a professional level, I don't write useless classes.
If you have ever worked in a test driven development environment that utilises automated testing there is often a need to access properties from Web.Config in console / windows apps. Now as you point out "myapp.exe.config" all that is built in for you, BUT I don't want to replicate settings from Web.Config in myapp.exe.config, i want a single config file that holds all the settings required.
If you still don't understand I'll send you some more details.
|
|
|
|
 |
|
 |
Hi,
Nice class indeed. I use a Winforms interface to manage webapp security. The alternative is creating a web.config and an app.config, with both settings synchronized. I don't like this! So drop the useless crap please.
|
|
|
|
 |
|
 |
Nice class. answer my need !
|
|
|
|
 |
|
 |
cgreen69 wrote: If you have ever worked in a test driven development environment that utilises automated testing there is often a need to access properties from Web.Config in console / windows apps. Now as you point out "myapp.exe.config" all that is built in for you, BUT I don't want to replicate settings from Web.Config in myapp.exe.config, i want a single config file that holds all the settings required.
I agree with your "keep it professional," and am not the original poster on this thread. However, the Framework can reference external XML files from the appSettings system, which can accomplish what you are doing here (IE sharing configuration between a console, service and WebApp).
To reference an external file from your application settings section, you make your config file look like this:
<configuration>
<system.web>
<compilation defaultLanguage="c#" debug="true" />
</system.web>
<appSettings file="c:\Program Files\MyCommonSettings\Common.config"/>
</configuration>
This tells the framework to load the appsettings from an external configuration file. The file then looks something like this:
<appSettings>
<add key="ConnectionInfo" value="server=(local);database=Northwind;Integrated Security=SSPI" />
</appSettings>
Note that all that this has done is moved the appSettings section to an external file. You can reference that file from as many other config files as you please, file system permissions permitting.
There are times when you can't use System.Configuration, however, for other reasons. For example, you might be a server control running in the IDE and needing to get at web.config in order to read a URL from the file (we've had this situation come up a few times now). What we've typically done is appKey*defaultValue, but that is ugly.
With this class, it is possible to request the project path from the IDE and to use that to find Web.config, and then process it, letting the server control have access to the values in design mode.
|
|
|
|
 |
|
 |
I do the same thing so I can write utility applications that are driven by the settings in my ASP.NET sites. If I update a connection string or password in the web.config then my helper services will automatically grab the same updated information.
cheers,
Chris Maunder
|
|
|
|
 |
|
 |
But why?
For me, my helper applications (usually console applications) have their own ".exe.config" files. I see benefits of totally independent applications, compared with sharing the configuration files.
Are your helper classes capable of dealing with custom config-sections, too? Personally, I dislike lare entries of "appSettings" items and prefer making a more structured section of my own settings.
--
- Free Windows-based CMS: www.zeta-software.de/enu/producer/freeware/download.html
- See me: www.magerquark.de
- MSN Messenger: uwe_keim@hotmail.com
|
|
|
|
 |
|
 |
Okay, here goes.
You have a solution that comprises a web project and a windows based automated testing tool such as NUnit.
Due to the windows based nature of NUnit the web context is unavailable. In order to instantiate database connection strings and so forth that would normally be available in web.config you would normally have to hard code them into the test classes.
My class allows the settings in Web Config to be exposed in the windows environemnt without the need for hard coding.
App.Config is not viable because a) the tool does not recognise it and b) why duplicate data in two different files for the same solution.
If you haven't worked on solutions that combine web and windows / console projects you may not appreciate the need to do this, but its a real time saver for those of us that do.
|
|
|
|
 |