我注意到在一些機器中,我的應用程序的user.config文件在某種程度上被損壞,並且在打開時爲空。我似乎無法弄清楚爲什麼會發生這種情況。有沒有共同的事情會導致這種情況?任何方式來安全地防止這一點?是什麼導致user.config爲空?如何在不重新啓動的情況下恢復?
我的第二個問題是如何恢復狀態?我發現異常並刪除了user.config文件,但找不到重新啓動應用程序的情況下恢復配置的方法。一切我Properties對象上做導致以下錯誤:
「配置系統初始化失敗」
復位,刷新和升級都無助於解決問題。
這裏是我的異常後刪除代碼:
catch (System.Configuration.ConfigurationErrorsException ex)
{
string fileName = "";
if (!string.IsNullOrEmpty(ex.Filename))
fileName = ex.Filename;
else
{
System.Configuration.ConfigurationErrorsException innerException = ex.InnerException as System.Configuration.ConfigurationErrorsException;
if (innerException != null && !string.IsNullOrEmpty(innerException.Filename))
fileName = innerException.Filename;
}
if (System.IO.File.Exists(fileName))
System.IO.File.Delete(fileName);
}
如果user.config文件corrrupt,你必須重新啓動,只要你實際存儲設置的文件中值。您必須重新啓動的原因很明顯,如果文件損壞,您的應用程序處於不穩定狀態。 – 2012-03-05 18:57:07
這就是我想的。我希望有一種方法可以在運行時將應用程序配置恢復到默認設置而無需重啓 - 因爲重啓看起來非常不可靠。 – JeremyK 2012-03-05 19:00:25
我有一個應用程序不時出現同樣的問題。迄今爲止,我還沒有找到解決這個問題或解決這個問題的根本原因。該應用程序已從.NET v3.5移至v4.0,因此該問題在v2.0和v4.0運行時均存在。可能的重複:http://stackoverflow.com/questions/3071651/how-to-catch-exception-when-loading-net-winform-app-user-config-file – harlam357 2013-02-25 14:47:16