Runtime Web.config / App.config Editing

该博客展示了一段C#代码,实现了对AppConfig配置文件的操作。定义了ConfigFileType枚举,包含WebConfig和AppConfig类型。AppConfig类继承自System.Configuration.AppSettingsReader,提供了设置值、移除元素等方法,通过XmlDocument进行配置文件的读写操作。
摘自http://www.eggheadcafe.com/articles/20030907.asp
using System;
using System.Xml;  
using System.Configuration;
using System.Collections;
using System.Reflection;  
using System.Diagnostics ;

public enum   ConfigFileType
{
 WebConfig ,
 AppConfig
}

public class AppConfig : System.Configuration.AppSettingsReader
{ 
 public string  docName = String.Empty;
 private  XmlNode node=null;

 private int _configType;
public int ConfigType { get { return _configType; } set { _configType=value; } } public bool SetValue(string key, string value) { XmlDocument cfgDoc = new XmlDocument(); loadConfigDoc(cfgDoc); // retrieve the appSettings node node = cfgDoc.SelectSingleNode("//appSettings"); if( node == null ) { throw new System.InvalidOperationException( "appSettings section not found"); } try { // XPath select setting "add" element that contains this key XmlElement addElem= (XmlElement)node.SelectSingleNode("//add[@key='" +key +"']") ; if (addElem!=null) { addElem.SetAttribute("value",value); } // not found, so we need to add the element, key and value else { XmlElement entry = cfgDoc.CreateElement("add"); entry.SetAttribute("key",key); entry.SetAttribute("value",value); node.AppendChild(entry); } //save it saveConfigDoc(cfgDoc,docName); return true; } catch { return false; } } private void saveConfigDoc(XmlDocument cfgDoc,string cfgDocPath) { try { XmlTextWriter writer = new XmlTextWriter( cfgDocPath , null ); writer.Formatting = Formatting.Indented; cfgDoc.WriteTo( writer ); writer.Flush(); writer.Close(); return; } catch { throw; } } public bool removeElement ( string elementKey) { try { XmlDocument cfgDoc = new XmlDocument(); loadConfigDoc(cfgDoc); // retrieve the appSettings node node = cfgDoc.SelectSingleNode("//appSettings"); if( node == null ) { throw new System.InvalidOperationException( "appSettings section not found"); } // XPath select setting "add" element that contains this key to remove node.RemoveChild( node.SelectSingleNode("//add[@key='" +elementKey +"']") ); saveConfigDoc(cfgDoc,docName); return true; } catch { return false; } } private XmlDocument loadConfigDoc( XmlDocument cfgDoc ) { // load the config file if( Convert.ToInt32(ConfigType)==Convert.ToInt32(ConfigFileType.AppConfig)) { docName= ((Assembly.GetEntryAssembly()).GetName()).Name; docName += ".exe.config"; } else { docName=System.Web.HttpContext.Current.Server.MapPath("web.config"); } cfgDoc.Load( docName ); return cfgDoc; } }
// Jitsi Meet configuration. var config = {}; config.hosts = {}; config.hosts.domain = 'meet.jitsi'; var subdir = '<!--# echo var="subdir" default="" -->'; var subdomain = '<!--# echo var="subdomain" default="" -->'; if (subdir.startsWith('<!--')) { subdir = ''; } if (subdomain) { subdomain = subdomain.substring(0,subdomain.length-1).split('.').join('_').toLowerCase() + '.'; } config.hosts.muc = 'muc.' + subdomain + 'meet.jitsi'; config.bosh = 'https://mt.zy5158.com/' + subdir + 'http-bind'; config.bridgeChannel = { preferSctp: true }; // Video configuration. // config.resolution = 720; config.constraints = { video: { height: { ideal: 720, max: 720, min: 180 }, width: { ideal: 1280, max: 1280, min: 320}, } }; config.startVideoMuted = 10; config.startWithVideoMuted = false; config.flags = { sourceNameSignaling: true, sendMultipleVideoStreams: true, receiveMultipleVideoStreams: true }; // ScreenShare Configuration. // // Audio configuration. // config.enableNoAudioDetection = true; config.enableTalkWhileMuted = false; config.disableAP = false; config.disableAGC = false; config.audioQuality = { stereo: false }; config.startAudioOnly = false; config.startAudioMuted = 10; config.startWithAudioMuted = false; config.startSilent = false; config.enableOpusRed = false; config.disableAudioLevels = false; config.enableNoisyMicDetection = true; // Peer-to-Peer options. // config.p2p = { enabled: true, codecPreferenceOrder: ["AV1", "VP9", "VP8", "H264"], mobileCodecPreferenceOrder: ["VP8", "VP9", "H264", "AV1"] }; // Breakout Rooms // config.hideAddRoomButton = false; // Etherpad // // Recording. // // Local recording configuration. config.localRecording = { disable: true, notifyAllParticipants: false, disableSelfRecording: false }; // Analytics. // config.analytics = {}; // Dial in/out services. // // Calendar service integration. // config.enableCalendarIntegration = false; // Invitation service. // // Miscellaneous. // // Prejoin page. config.prejoinConfig = { enabled: true, // Hides the participant name editing field in the prejoin screen. hideDisplayName: false }; // List of buttons to hide from the extra join options dropdown on prejoin screen. // Welcome page. config.welcomePage = { disabled: false }; // Close page. config.enableClosePage = false; // Default language. // Require users to always specify a display name. config.requireDisplayName = false; // Chrome extension banner. // Disables profile and the edit of all fields from the profile settings (display name and email) config.disableProfile = false; // Room password (false for anything, number for max digits) config.roomPasswordNumberOfDigits = false; // Advanced. // // Transcriptions (subtitles and buttons can be configured in interface_config) config.transcription = { enabled: false, disableClosedCaptions: true, translationLanguages: [], translationLanguagesHead: ['en'], useAppLanguage: true, preferredLanguage: 'en-US', disableStartForAll: false, autoCaptionOnRecord: false, }; // Dynamic branding // Deployment information. // config.deploymentInfo = {}; // Deep Linking config.disableDeepLinking = false; // P2P preferred codec // Video quality settings. // config.videoQuality = {}; config.videoQuality.codecPreferenceOrder = ["AV1", "VP9", "VP8", "H264"]; config.videoQuality.mobileCodecPreferenceOrder = ["VP8", "VP9", "H264", "AV1"]; config.videoQuality.enableAdaptiveMode = true; config.videoQuality.av1 = {}; config.videoQuality.h264 = {}; config.videoQuality.vp8 = {}; config.videoQuality.vp9 = {}; // Reactions config.disableReactions = false; // Polls config.disablePolls = false; // Configure toolbar buttons // Hides the buttons at pre-join screen // Configure remote participant video menu config.remoteVideoMenu = { disabled: false, disableKick: false, disableGrantModerator: false, disablePrivateChat: false }; // Configure e2eping config.e2eping = { enabled: false }; // Settings for the Excalidraw whiteboard integration. config.whiteboard = { enabled: false, }; // JaaS support: pre-configure image if JAAS_APP_ID was set. // Testing config.testing = { enableCodecSelectionAPI: true }; 解答下
最新发布
09-18
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值