O__o 這...發生了什麼事?
趕緊回頭查程式碼,哪裡會產生例外錯誤我來攔一下,不過~找不太到 XD
後來請探子再送來這支程式的 .config 之後,終於知道問題在哪了。
以下亂寫一個模擬用的小小主控台應用程式(Console Application)
App.config /
1 2 3 4 5 6 7 8 9 | <? xml version = "1.0" ?> < configuration > < appSettings > < add key = "thisIs543" value = "枸細殺" /> </ appSettings > < startup > < supportedRuntime version = "v4.0" sku = ".NETFramework,Version=v4.0" /> </ startup > </ configuration > |
1 2 3 4 5 | string value = ConfigurationManager.AppSettings[ "thisIs543" ]; Console.WriteLine( "using '{0}' do something..." , value); Console.WriteLine( "I will close after 15 sec." ); System.Threading.Thread.Sleep(15000); |
再來,模擬探子送來的 .config 內容
App.config /
1 2 3 4 5 6 7 8 9 10 | <? xml version = "1.0" ?> < configuration > < appSettings > < add key = "thisIs543" value = "枸細殺" /> </ appSettings > < startup > < supportedRuntime version = "v4.0" sku = ".NETFramework,Version=v4.0" /> </ startup > </ configuration > |
真~壞了!
哪裡不一樣?請再看看兩個 App.config 內容,一個9行字,一個10行字。
結果就是後來多那第一行空行造成的。但,造成什麼錯誤呢?再改一下程式
CS /
1 2 3 4 5 6 7 8 9 10 | string value = "" ; try { value = ConfigurationManager.AppSettings[ "thisIs543" ]; } catch (Exception ex) { Console.WriteLine(ex.ToString()); } |
其實透過 Visual Studio 執行的話,會提早出現相同的錯誤訊息(所以撰寫程式的人應該不曾發生過這樣的問題)
最後,對應主題的答案就是,把空格加在 .config 內容中 <?xml 的前面即可^^
App.config /
1 2 3 4 5 6 7 8 9 | <? xml version = "1.0" ?> < configuration > < appSettings > < add key = "thisIs543" value = "枸細殺" /> </ appSettings > < startup > < supportedRuntime version = "v4.0" sku = ".NETFramework,Version=v4.0" /> </ startup > </ configuration > |
我之前也有遇過這問題
回覆刪除我的記法是把
當成 META DATA 。
沒事還是不要亂動他,科科
這次有讓我嚇一跳,希望下次我還記得這狀況
回覆刪除