2009-06-12 23 views
0

我需要在web服务中向AppSettings添加自定义值在webservice中为web.config添加值

我有这段代码,但没有任何反应。

procedure TWebService1.AddStrConn(KeyConn, ValueConn: String); 
var 
config : System.Configuration.Configuration; 
begin 
config:=ConfigurationManager.OpenExeConfiguration(System.Reflection.Assembly.GetExecutingAssembly().Location); 
config.AppSettings.Settings.Add(KeyConn,ValueConn); 
config.Save(ConfigurationSaveMode.Modified); 
ConfigurationManager.RefreshSection('appSettings'); 
end; 

也尽量

procedure TWebService1.AddStrConn(KeyConn, ValueConn: String); 
var 
config : System.Configuration.Configuration; 
begin 
config:=ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); 
config.AppSettings.Settings.Add(KeyConn,ValueConn); 
config.Save(ConfigurationSaveMode.Modified); 
ConfigurationManager.RefreshSection('appSettings'); 
end; 

回答

4

您正在使用OpenExeConfiguration,其意在* .exe.config。要打开web.config,请尝试类似

Configuration cfg = WebConfigurationManager.OpenWebConfiguration("~"); 

它应该允许您保存,前提是您的服务有权这样做。

+0

非常感谢。 – RRUZ 2009-06-12 08:36:09