c# - How to simulate the file attribute of a .config file's appSettings section -
microsoft gives instructions how use file
attribute make .config
file's appsettings
section reference file: example:
<appsettings file="c:\commonsettings.config"> <add key="myappspecificsetting" value="setting1" /> </appsettings>
however, implemented appsettings
, isn't general .config
file feature.
now, have implemented own configurationsection
, , able specify file=
1 can appsettings
. aside creating property match attribute , retrieve file name, how can recreate/simulate functionality in custom configsection
?
thankfully have the source code now. looks it's implemented appsettingssection
reading file system, nothing special can reuse in own configuration section.
protected internal override void deserializeelement(xmlreader reader, bool serializecollectionkey) { string elementname = reader.name; base.deserializeelement(reader, serializecollectionkey); if ((file != null) && (file.length > 0)) { string sourcefilefullpath; string configfiledirectory; string configfile; // determine file location configfile = elementinformation.source; if (string.isnullorempty(configfile)) { sourcefilefullpath = file; } else { configfiledirectory = system.io.path.getdirectoryname(configfile); sourcefilefullpath = system.io.path.combine(configfiledirectory, file); } if (system.io.file.exists(sourcefilefullpath)) { int lineoffset = 0; string rawxml = null; using (stream sourcefilestream = new filestream(sourcefilefullpath, filemode.open, fileaccess.read, fileshare.read)) { using (xmlutil xmlutil = new xmlutil(sourcefilestream, sourcefilefullpath, true)) { if (xmlutil.reader.name != elementname) { throw new configurationerrorsexception( sr.getstring(sr.config_name_value_file_section_file_invalid_root, elementname), xmlutil); } lineoffset = xmlutil.reader.linenumber; rawxml = xmlutil.copysection(); // detect if there xml left on after section while (!xmlutil.reader.eof) { xmlnodetype t = xmlutil.reader.nodetype; if (t != xmlnodetype.comment) { throw new configurationerrorsexception(sr.getstring(sr.config_source_file_format), xmlutil); } xmlutil.reader.read(); } } } configxmlreader internalreader = new configxmlreader(rawxml, sourcefilefullpath, lineoffset); internalreader.read(); if (internalreader.movetonextattribute()) { throw new configurationerrorsexception(sr.getstring(sr.config_base_unrecognized_attribute, internalreader.name), (xmlreader)internalreader); } internalreader.movetoelement(); base.deserializeelement(internalreader, serializecollectionkey); } } }
Comments
Post a Comment