У меня был когда-то проект - осталось вот это. Могу просто предложить покопаться
Вот конфиг в виде XML. Первая половина описывает те параметры (тэги), которые будут использоваться. Вторая половина - это уже непосредственно сами параметры.
| Код | <?xml version="1.0" encoding="windows-1251"?> <configuration> <configSections> <sectionGroup name="PipetteSectionGroup"> <section name="PipetteName" type="System.Configuration.SingleTagSectionHandler"/> <section name="SerialPortSection" type="System.Configuration.NameValueSectionHandler"/> <section name="DataBaseConnection" type="System.Configuration.SingleTagSectionHandler"/> <section name="FlashConnection" type="System.Configuration.SingleTagSectionHandler"/> <section name="PipetteConnection" type="System.Configuration.SingleTagSectionHandler"/> <section name="EmbeddedSoft" type="System.Configuration.SingleTagSectionHandler"/> <section name="Debug" type="System.Configuration.SingleTagSectionHandler"/> </sectionGroup> </configSections> <appSettings> </appSettings> <PipetteSectionGroup> <PipetteName name="NOVUS"/> <SerialPortSection> <add key="COM1" value="1"/> <!--add key="COM2" value="2"/> <add key="COM4" value="4"/--> </SerialPortSection> <DataBaseConnection source="MSAccess" connect="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Pipetka.MDB"/> <!--DataBaseConnection source="SQLServer" connect="Server=(local);Database=Pipetka;Password=;User ID=sa"/--> <FlashConnection baud="115200"/> <PipetteConnection baud="9600"/> <EmbeddedSoft file="extens.xcl" dir="NEC"/> <Debug serial_log_flash="0" serial_log_remote="0"/> </PipetteSectionGroup> </configuration>
|
А вот и текст для работы с ним
| Код | public class Config_Pipette { public static string GetPipetteName() { IDictionary sampleTable; try { sampleTable = (IDictionary) ConfigurationSettings.GetConfig("PipetteSectionGroup/PipetteName"); } catch(ConfigurationException con_e) { throw new Exceptions.BadConfigException("Config file error:"+con_e.Message); } if(sampleTable==null) { throw new Exceptions.NoConfigException("Config file doesn't exist"); } return (string)sampleTable["name"]; }
public static Array GetSerialPorts() { ArrayList tmp = new ArrayList(); NameValueCollection nvc = null; try { nvc = (NameValueCollection) ConfigurationSettings.GetConfig("PipetteSectionGroup/SerialPortSection"); } catch(ConfigurationException con_e) { throw new Exceptions.BadConfigException("Config file error:"+con_e.Message); } if(nvc==null) { throw new Exceptions.NoConfigException("Config file doesn't exist"); } else { for(int i=0; i<nvc.Count; i++) { tmp.Add(int.Parse(nvc[i])); } } return tmp.ToArray(); }
public static void GetDataBaseConnection(out string source, out string connect) { IDictionary sampleTable; try { sampleTable = (IDictionary) ConfigurationSettings.GetConfig("PipetteSectionGroup/DataBaseConnection"); } catch(ConfigurationException con_e) { throw new Exceptions.BadConfigException("Config file error:"+con_e.Message); } if(sampleTable==null) { throw new Exceptions.NoConfigException("Config file doesn't exist"); } source = (string)sampleTable["source"]; connect = (string)sampleTable["connect"]; }
public static string GetFlashBaudRate() { IDictionary sampleTable; try { sampleTable = (IDictionary) ConfigurationSettings.GetConfig("PipetteSectionGroup/FlashConnection"); } catch(ConfigurationException con_e) { throw new Exceptions.BadConfigException("Config file error:"+con_e.Message); } if(sampleTable==null) { throw new Exceptions.NoConfigException("Config file doesn't exist"); } return (string)sampleTable["baud"]; }
public static string GetPipetteBaudRate() { IDictionary sampleTable; try { sampleTable = (IDictionary) ConfigurationSettings.GetConfig("PipetteSectionGroup/PipetteConnection"); } catch(ConfigurationException con_e) { throw new Exceptions.BadConfigException("Config file error:"+con_e.Message); } if(sampleTable==null) { throw new Exceptions.NoConfigException("Config file doesn't exist"); } return (string)sampleTable["baud"]; }
public static string GetEmbeddedSoftDir() { IDictionary sampleTable; try { sampleTable = (IDictionary) ConfigurationSettings.GetConfig("PipetteSectionGroup/EmbeddedSoft"); } catch(ConfigurationException con_e) { throw new Exceptions.BadConfigException("Config file error:"+con_e.Message); } if(sampleTable==null) { throw new Exceptions.NoConfigException("Config file doesn't exist"); } return (string)sampleTable["dir"]; }
public static string GetEmbeddedSoftFileExt() { IDictionary sampleTable; try { sampleTable = (IDictionary) ConfigurationSettings.GetConfig("PipetteSectionGroup/EmbeddedSoft"); } catch(ConfigurationException con_e) { throw new Exceptions.BadConfigException("Config file error:"+con_e.Message); } if(sampleTable==null) { throw new Exceptions.NoConfigException("Config file doesn't exist"); } return (string)sampleTable["file"]; }
public static bool GetDebugSerialLogFlash() { IDictionary sampleTable; try { sampleTable = (IDictionary) ConfigurationSettings.GetConfig("PipetteSectionGroup/Debug"); } catch(ConfigurationException con_e) { throw new Exceptions.BadConfigException("Config file error:"+con_e.Message); } if(sampleTable==null) { throw new Exceptions.NoConfigException("Config file doesn't exist"); } string tmp_log = (string)sampleTable["serial_log_flash"]; return tmp_log.Equals("1") ? true : false; }
public static bool GetDebugSerialLogRemote() { IDictionary sampleTable; try { sampleTable = (IDictionary) ConfigurationSettings.GetConfig("PipetteSectionGroup/Debug"); } catch(ConfigurationException con_e) { throw new Exceptions.BadConfigException("Config file error:"+con_e.Message); } if(sampleTable==null) { throw new Exceptions.NoConfigException("Config file doesn't exist"); } string tmp_log = (string)sampleTable["serial_log_remote"]; return tmp_log.Equals("1") ? true : false; } }
| |