Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Configuration File - Dynamic settings

Status
Not open for further replies.

rf222

Programmer
Jan 29, 2004
94
US
Hi, I am aware off <APPSettings>, where you can define Key and Value....

However I would like to be able to have my own section in web.config, called <InputFiles>. Here would be an example:

<InputFiles>
<add name="myfile1.txt"
Regex="__(\S)"
/>
<add name="myfile2.txt"
Regex="(__\S)"
/>

</InputFiles>

1) Is it possible to use my own custom section like this with different attributes? Where would I place it in web.config?

2) How would I read it in my C# application? The number of files (myfilex.txt) can be unlimited...

Please help. Thanks
 
Thanks a lot! However I heard there may be another (simpler) way to do this in ASP 2.0. I saw same example, where class was derived from ConfigurationSection class, however it did not serve my purpose, because the format of nodes was as follows:

<orderservice attr1="blah, blah" attr2="blah" />

so I would like to have subnodes like this:

<InputFiles>
<add name="myfile1.txt"
Regex="__(\S)"
/>
<add name="myfile2.txt"
Regex="(__\S)"
/>
 
oh no, u can also have sub sections dude. u need to read the web.config tutorials for this, and regarding .NET 2, i still dont have much of an idea there...

Known is handfull, Unknown is worldfull
 
It is very confusing on MSDN. There are several classes. I found ConfigurationSection (2.0 new class), byt not sure if I can use it with my collection:

<InputFiles>
<add name="myfile1.txt"
Regex="__(\S)"
/>
<add name="myfile2.txt"
Regex="(__\S)"
/>

</InputFiles>

Seems like it only takes care of this kind of config info:

<CustomSection fileName="default.txt" maxUsers="1000" maxIdleTime="00:15:00" />

That is a single element.... I need several dynamic, so the user can add them later. It would be a Collection....
Here is the link to the class I am taking about:


There are other classes there too:

So I am not sure which one to use win ma case. Wonder if somebody was using those new classes to read config info like mine.
 
The tutorial you have provided is very nice, However it only reads single element. I can have several elements:

<InputFiles>
<add name="myfile1.txt" Regex="__(\S)"/>
<add name="myfile2.txt" Regex="(__\S)"/>

How do I read those? I feel I may need to return the collection...
 
yeah, u have to return a collection, just give me some time so that i can cook up some code for u...

Known is handfull, Unknown is worldfull
 
Hi, I found some sample (net 1) and I have modified it. However seems like web.config is not calling my section handler. I put some code to generate error there, seems it is never called.... Here is web.config:



<configSections>


<section name="ErrorLogs" type="BLA.ErrLog.ErrLogConfigurationHandler, ErrLogConfiguration"/>

...

<ErrorLogs name="Input Files Configuration" tracingEnabled="true">

<add name="R:\\Applications\\MISDesktop\\Error.log" regex=""/>

<add name="GLOPENU2.TXT" regex=""/>

<add name="GTIRTNS.TXT" regex=""/>

</ErrorLogs>



Here is the code of the handler (never called...):



using

System;
using

System.Configuration;
using

System.Xml;
namespace

BLA.ErrLog {

/// <summary>

/// Summary description for ContentConfigurationHandler.

/// </summary>

public class ErrLogConfigurationHandler : IConfigurationSectionHandler {

public virtual object Create(Object parent, Object context, XmlNode node) {

ErrLogConfiguration config = new ErrLogConfiguration();
config.LoadValuesFromConfigurationXml(node);


int j = 0;

int i = 9 / j;

return config;
}

}

}


Here is another file with ErrLog Class:

using System;
using System.Configuration;
using System.Collections;
using System.Xml;
using System.Collections.Specialized;

namespace BLA.ErrLog {

public class ErrLogConfiguration {
string defaultErrLog;
string inputDirectory;
string outputDirectory;

Hashtable errLogs = new Hashtable();

public static ErrLogConfiguration GetConfig() {
return (ErrLogConfiguration)ConfigurationSettings.GetConfig("ErrLog");
}

public void LoadValuesFromConfigurationXml(XmlNode node) {
XmlAttributeCollection attributeCollection = node.Attributes;

// Get the default ErrLog
defaultErrLog = attributeCollection["defaultErrLog"].Value;
inputDirectory = attributeCollection["InputDirectory"].Value;
outputDirectory = attributeCollection["OutputDirectory"].Value;

// Read child nodes
foreach (XmlNode child in node.ChildNodes) {
if (child.Name == "ErrLogs")
GetErrLogs(child);
}
}

void GetErrLogs(XmlNode node) {
foreach (XmlNode ErrLog in node.ChildNodes) {
switch (ErrLog.Name) {
case "add" :
errLogs.Add(ErrLog.Attributes["name"].Value, new ErrLog(ErrLog.Attributes) );
break;

case "remove" :
errLogs.Remove(ErrLog.Attributes["name"].Value);
break;

case "clear" :
errLogs.Clear();
break;
}
}
}

// Properties
//
public string DefaultErrLog { get { return defaultErrLog; } }
public string InputDirectory { get { return inputDirectory; } }
public string OutputDirectory { get { return outputDirectory; } }
public Hashtable ErrLogs { get { return errLogs; } }

}

public class ErrLog {
string name;
string ErrLogType;

NameValueCollection ErrLogAttributes = new NameValueCollection();

public ErrLog (XmlAttributeCollection attributes) {

// Set the name of the ErrLog
//
name = attributes["name"].Value;

// Set the type of the ErrLog
//
ErrLogType = attributes["regex"].Value;

// Store all the attributes in the attributes bucket
//
foreach (XmlAttribute attribute in attributes) {

if ( (attribute.Name != "name") && (attribute.Name != "regex") )
ErrLogAttributes.Add(attribute.Name, attribute.Value);

}

}

public string Name {
get {
return name;
}
}

public string Type {
get {
return ErrLogType;
}
}

public NameValueCollection Attributes {
get {
return ErrLogAttributes;
}
}

}

}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top