我已經得到了功能在web.config中 改變價值觀,但我的問題是,它沒有得到正確的web.config的路徑和投擲的web.config在asp.net
「找不到文件C:\用戶\ maxnet25 \ Web.config文件'」 它給錯誤的xmlDoc.Load()函數。
我的代碼:
public void UpdateConfigKey(string strKey, string newValue)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(AppDomain.CurrentDomain.BaseDirectory + "..\\..\\Web.config");
if (!ConfigKeyExists(strKey))
{
throw new ArgumentNullException("Key", "<" + strKey + "> not find in the configuration.");
}
XmlNode appSettingsNode = xmlDoc.SelectSingleNode("configuration/appSettings");
foreach (XmlNode childNode in appSettingsNode)
{
if (childNode.Attributes["key"].Value == strKey)
childNode.Attributes["value"].Value = newValue;
}
xmlDoc.Save(AppDomain.CurrentDomain.BaseDirectory + "..\\..\\Web.config");
xmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
Label1 .Text ="Key Upated Successfullly";
}
請問您可以添加完整的例外詳情嗎? – MPritchard 2010-07-29 12:56:22
還有一些關於你的web項目文件夾結構的信息......是在IIS還是dev-server下運行的?通常情況下,web.config應該放在AppDomain.CurrentDomain.BaseDirectory下...如果您已經在「C:\ inetpub \ wwwroot」中安裝了應用程序,那麼在IIS上,您的路徑將導致「C:\ web.config」。 .. – OlafW 2010-07-29 13:00:28
@MPritch - 我懷疑'無法找到文件'是異常消息,幾乎可以肯定是因爲'.. \\ .. \\'在目錄樹下太遠了,因爲它是不太可能的OP將其Web應用程序存儲在C:\ Users \ maxnet25 \ Web.config中。更可能的是C:\ Users \ maxnet25 \ Documents \ MyWebApp \ Web.config,這將是代碼看起來如果不是.. \\ .. \\ =) – Rob 2010-07-29 13:02:25