积极答复者
封装类库,如何实现引用时自动向Web.config中添加节点

问题
答案
-
是阿,我了解你的目的,不過我沒寫過元件,不曉得怎麼達成它。
只不過是提出個不同想法,您參考看看。
關於重複寫入的問題,您可以在專案裡添加個Global.asax,
然後在Application_Start事件裡這樣寫試試:
void Application_Start(object sender, EventArgs e) { Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(HttpRuntime.AppDomainAppVirtualPath); AppSettingsSection app = config.AppSettings; if (!app.Settings.AllKeys.Contains("B")) { app.Settings.Add("B", "This is B value"); config.Save(ConfigurationSaveMode.Modified); } }
如此只有在站點啟動之後才會進來這麼一次,
寫入之後也不會再重複寫入了。
Best regards- 已编辑 DK. Da 2013年4月24日 3:14
- 已建议为答案 ThankfulHeart 2013年4月24日 9:43
- 已标记为答案 Song TianModerator 2013年4月29日 8:58
全部回复
-
我不曉得引用時如何添加,
不過找到了in Code的做法,
你可以在使用到類庫的Code裡先做這件事,
或是在站點進入頁面做,
參考看看:
[ASP.NET][C#]app.config與web.config的增加、修改、刪除操作
Best regards -
是阿,我了解你的目的,不過我沒寫過元件,不曉得怎麼達成它。
只不過是提出個不同想法,您參考看看。
關於重複寫入的問題,您可以在專案裡添加個Global.asax,
然後在Application_Start事件裡這樣寫試試:
void Application_Start(object sender, EventArgs e) { Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(HttpRuntime.AppDomainAppVirtualPath); AppSettingsSection app = config.AppSettings; if (!app.Settings.AllKeys.Contains("B")) { app.Settings.Add("B", "This is B value"); config.Save(ConfigurationSaveMode.Modified); } }
如此只有在站點啟動之後才會進來這麼一次,
寫入之後也不會再重複寫入了。
Best regards- 已编辑 DK. Da 2013年4月24日 3:14
- 已建议为答案 ThankfulHeart 2013年4月24日 9:43
- 已标记为答案 Song TianModerator 2013年4月29日 8:58
-
是阿,我了解你的目的,不過我沒寫過元件,不曉得怎麼達成它。
只不過是提出個不同想法,您參考看看。
關於重複寫入的問題,您可以在專案裡添加個Global.asax,
然後在Application_Start事件裡這樣寫試試:
void Application_Start(object sender, EventArgs e) { Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(HttpRuntime.AppDomainAppVirtualPath); AppSettingsSection app = config.AppSettings; if (!app.Settings.AllKeys.Contains("B")) { app.Settings.Add("B", "This is B value"); config.Save(ConfigurationSaveMode.Modified); } }
如此只有在站點啟動之後才會進來這麼一次,
寫入之後也不會再重複寫入了。
Best regards