本文介绍了C#从第三方app.config获取并设置用户定义的部分信息(监听器)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 您好, 我需要获取侦听器信息的部分(name,toAddress,fromAddress,subjectLineStarter,subjectLineEnder,smtpServer,smtpPort,filter,rollSizeKB,fileName)从第三方app.config并将它们保存到集合中(例如:array,List ...),这样我就可以通过在WinForm中更改相应的textBox.text来修改不同的属性值。最好的方法是什么? b $ b Hello,I need to get the listeners information's section (name, toAddress,fromAddress, subjectLineStarter, subjectLineEnder, smtpServer,smtpPort, filter, rollSizeKB, fileName) from a third party app.config and save them into collection (example: array, List...), so I can modify the different attributes values by changing the corresponding textBox.text in my WinForm What's the best way to do that?<?xml version="1.0"?><configuration> <configSections> <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=45fr1254ee458e89" requirePermission="true" /> <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> <section name="Dds.BiBoard.Proxies.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> </sectionGroup> </configSections> <loggingConfiguration name="ITE Logging Application Block" tracingEnabled="true" defaultCategory="Info" logWarningsWhenNoCategoriesMatch="true"> <listeners> <add name="IteProjectEmailAdministrator" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.EmailTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=45fr1254ee458e89" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.EmailTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=45fr1254ee458e89" toAddress="[email protected]" fromAddress="[email protected]" subjectLineStarter="ITEProject Critical Logs" subjectLineEnder="Urgent" smtpServer="smtp.zz.net" smtpPort="25" formatter="Text Formatter" traceOutputOptions="None" filter="Off" /> <add name="IteProjectErrorRollingFlatFile" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=45fr1254ee458e89" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=45fr1254ee458e89" fileName="C:\logs\LogIteProjectError.log" footer="" formatter="Text Formatter" header="" rollFileExistsBehavior="Increment" rollInterval="None" rollSizeKB="100" timeStampPattern="yyyy-MM-dd" traceOutputOptions="None" filter="Error" /> <add name="IteProjectExceptionRollingFlatFile" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=45fr1254ee458e89" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=45fr1254ee458e89" fileName="C:\logs\LogIteProjectException.log" footer="" formatter="Text Formatter" header="" rollFileExistsBehavior="Increment" rollInterval="None" rollSizeKB="100" timeStampPattern="yyyy-MM-dd" traceOutputOptions="None" filter="All" /> <add name="IteProjectInformationRollingFlatFile" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=45fr1254ee458e89" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=45fr1254ee458e89" fileName="C:\logs\LogIteProjectInfo.log" footer="" formatter="Text Formatter" header="" rollFileExistsBehavior="Increment" rollInterval="None" rollSizeKB="100" timeStampPattern="yyyy-MM-dd" traceOutputOptions="None" filter="Information" /> <add name="IteProjectWarningRollingFlatFile" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=45fr1254ee458e89" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=45fr1254ee458e89" fileName="C:\logs\LogIteProjectWarning.log" footer="" formatter="Text Formatter" header="" rollFileExistsBehavior="Increment" rollInterval="None" rollSizeKB="100" timeStampPattern="yyyy-MM-dd" traceOutputOptions="None" filter="Warning" /> <add name="IteProjectCriticalRollingFlatFile" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=45fr1254ee458e89" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=45fr1254ee458e89" fileName="C:\logs\LogIteProjectCritical.log" footer="" formatter="Text Formatter" header="" rollFileExistsBehavior="Increment" rollInterval="None" rollSizeKB="100" timeStampPattern="yyyy-MM-dd" traceOutputOptions="None" filter="Critical" /> <add name="IteProjectDebugRollingFlatFile" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=45fr1254ee458e89" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=45fr1254ee458e89" fileName="C:\logs\LogIteProjectDebug.log" footer="" formatter="Text Formatter" header="" rollFileExistsBehavior="Increment" rollInterval="None" rollSizeKB="100" timeStampPattern="yyyy-MM-dd" traceOutputOptions="None" filter="Verbose" /> </listeners> <formatters> <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=45fr1254ee458e89" template="{timestamp(local)} - {severity} - {message}" name="Text Formatter" /> </formatters> <logFilters> <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.LogEnabledFilter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=45fr1254ee458e89" enabled="true" name="LogEnabled Filter" /> </logFilters> <categorySources> <add switchValue="All" name="Critical"> <listeners> <add name="IteProjectEmailAdministrator" /> <add name="IteProjectCriticalRollingFlatFile" /> </listeners> </add> <add switchValue="All" name="Error"> <listeners> <add name="IteProjectErrorRollingFlatFile" /> </listeners> </add> <add switchValue="All" name="Exception"> <listeners> <add name="IteProjectExceptionRollingFlatFile" /> </listeners> </add> <add switchValue="All" name="Info"> <listeners> <add name="IteProjectInformationRollingFlatFile" /> <add name="IteProjectWarningRollingFlatFile" /> </listeners> </add> <add switchValue="All" name="Warning"> <listeners> <add name="IteProjectWarningRollingFlatFile" /> <add name="IteProjectInformationRollingFlatFile" /> </listeners> </add> <add switchValue="All" name="Verbose"> <listeners> <add name="IteProjectDebugRollingFlatFile" /> </listeners> </add> </categorySources> <specialSources> <allEvents switchValue="All" name="All Events" /> <notProcessed switchValue="All" name="Unprocessed Category"> <listeners> <add name="IteProjectWarningRollingFlatFile" /> </listeners> </notProcessed> <errors switchValue="All" name="Logging Errors & Warnings"> <listeners> <add name="IteProjectErrorRollingFlatFile" /> </listeners> </errors> </specialSources> </loggingConfiguration> <connectionStrings> <add name="ITE_key" connectionString="Data Source=ITE;Persist Security Info=True;User ID=ITE_FIT;Password=*****;Unicode=True;" providerName="System.Data.OracleClient" /> </connectionStrings> <appSettings> <add key="ServiceName" value="test_Board-Serv" /> <add key="ProcessInterval" value="32" /> <add key="InsertArg" value="/I" /> <add key="UpdateArg" value="/U" /> <add key="DeleteArg" value="/D" /> <add key="ConsoleModeArg" value="/C" /> <add key="StartCountBeforeKill" value="3" /> <add key="ITE.Board.WcfService.BoardService.Url" value="http://localhost:83/BoardService.svc/" /> <add key="Default" value=" *" /> <add key="DIR_my_log" value="C:\Users\IT\Desktop\Log" /> <add key="FILE_my_fileLog" value="C:\Users\IT\Desktop\TestConfig\ITE-SV_Config.log" /> </appSettings> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" /> </startup> <system.serviceModel><big></big> <bindings> <basicHttpBinding> <binding name="BasicHttpBinding_BoardContract" /> </basicHttpBinding> </bindings> <client> <endpoint address="Ite.Board.WcfService.BoardService.Url" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_BoardContract" contract="IteBoard.Bunisess.Proxy.ITeBoardContract" name="BasicHttpBinding_IteBoardContract" /> </client> </system.serviceModel></configuration> Berst regards, ITBerst regards,IT推荐答案The easiest way is to simply parse the configuration file like a normal XML document. If you are looking for a more elegant solution, then there is a class defined to use and access this information. In the configSections, there are a series of entries that define classes that represent data in the configuration file. In the case of your configuration file, the class Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings was build to directly pull settings out of the configuration file. I personally have never worked with that class, so you will need to research the MSDN documentation on it and figure out how to make use of it.The easiest way is to simply parse the configuration file like a normal XML document.If you are looking for a more elegant solution, then there is a class defined to use and access this information. In the configSections, there are a series of entries that define classes that represent data in the configuration file. In the case of your configuration file, the class Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings was build to directly pull settings out of the configuration file. I personally have never worked with that class, so you will need to research the MSDN documentation on it and figure out how to make use of it.I’s solved by using Linq to xmlI's solved by using Linq to xml 这篇关于C#从第三方app.config获取并设置用户定义的部分信息(监听器)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-30 11:29