2010-07-29 35 views
1

我已經得到了功能在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";    
    } 
+0

請問您可以添加完整的例外詳情嗎? – MPritchard 2010-07-29 12:56:22

+0

還有一些關於你的web項目文件夾結構的信息......是在IIS還是dev-server下運行的?通常情況下,web.config應該放在AppDomain.CurrentDomain.BaseDirectory下...如果您已經在「C:\ inetpub \ wwwroot」中安裝了應用程序,那麼在IIS上,您的路徑將導致「C:\ web.config」。 .. – OlafW 2010-07-29 13:00:28

+0

@MPritch - 我懷疑'無法找到文件'是異常消息,幾乎可以肯定是因爲'.. \\ .. \\'在目錄樹下太遠了,因爲它是不太可能的OP將其Web應用程序存儲在C:\ Users \ maxnet25 \ Web.config中。更可能的是C:\ Users \ maxnet25 \ Documents \ MyWebApp \ Web.config,這將是代碼看起來如果不是.. \\ .. \\ =) – Rob 2010-07-29 13:02:25

回答

0

你有添加一個web.config到您的網站?

4

被賦予了什麼錯誤訊息話題?

無論哪種方式,你不是真的要對以正確的方式修改web.config文件。您應該看看System.Configuration.ConfigurationManager類,因爲它提供了以結構化方式對web.config文件的編程訪問。請注意,要訪問此類,您需要將對System.Configuration.dll的引用添加到項目中,以使ConfigurationManager成爲範圍。

如果您查看GetSection method的示例代碼,它將顯示如何在.net配置文件的appSettings部分中創建/添加設置,以便該示例應該足以讓您找到想要的位置。

如果您肯定想用這種方式來操縱你的web.config文件,我懷疑:

AppDomain.CurrentDomain.BaseDirectory + "..\\..\\Web.config") 

不正確,根據你已經在錯誤信息中顯示的路徑上。嘗試刪除.. \ .. \並看看是否有效。 AppDomain.CurrentDomain.BaseDirectory應該在web.config文件中不加修改的位置被人指指點點。

1

嘗試使用Server.MapPath()來解決你的web.config的位置。如果您在頁面中,Server是頁面屬性之一。如果沒有,你可以在HttpContext.Current找到它。

舉個例子...

HttpContext.Current.Server.MapPath("~/web.config") 

...應該在你的web應用程序的頂部返回web.config中的物理路徑。

現在,你可能使用過的WebConfigurationManager好得多,如圖this post。該方法更清潔,但需要參考System.Configuration

2

假設這確實是一個ASP.NET網站,而不是這樣的:

AppDomain.CurrentDomain.BaseDirectory + "..\\..\\Web.config" 

使用此:

HttpContext.Current.Server.MapPath("~/Web.config") 

在一個側面說明,請注意,任何時候你做出改變web.config,您的Web應用程序重新啓動。根據您的網絡應用程序的功能,您可能不需要擔心。對於應用

System.Configuration.ConfigurationManager 

+0

當處理appSettings時,您可以避免將appSettings存儲在外部配置文件中重新啓動。請參閱http://msdn.microsoft.com/en-us/library/ackhksh7.aspx。 – kbrimington 2010-07-29 13:06:02

+0

@kbrimington,但如果您在「網絡園」場景中運行,這意味着其他工作進程不會接受您的配置更改。值得銘記=) – Rob 2010-07-29 13:07:44

+0

感謝您的領導。 – kbrimington 2010-07-29 13:15:17

0

您應該使用。配置文件或:

System.Web.Configuration.WebConfigurationManager 

for web.config文件。

你實際上也可以使用System.Configuration.ConfigurationManager和web.config文件,老實說,我不確定是否有任何好處。

但無論如何,您不應該使用Xml命名空間並編寫/修改原始XML。