最近用戶反映一些電腦啟動程式就崩潰,還給演示了一個比較詭異的問題
“把軟體重新拷貝到另外一個目錄,就能正常運行",還說過一段時間又不能運行需要在換個位置,
’由于當時沒有設定全域例外,只能借助系統操作日志來分析, 系統日志記錄不全,就說發生一個例外程式掛掉,
就簡單加上全域例外捕獲,
public class ExceptionHelper { public static void InitException() { Application.Current.DispatcherUnhandledException += Application_DispatcherUnhandledException; AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; } private static void Application_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e) { LogManager.WriteException(e.Exception); e.Handled = true; } private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { var exception = e.ExceptionObject as Exception; if (exception != null) { LogManager.WriteException(exception); } } }
加入日志詳細資訊后,看到提示 Configuration system failed to initialize錯誤資訊,然后借助搜索,找到一篇文章,
了解到原因兩種:
一、config 檔案中 <configSections> 的位置順序有關或者內容格式錯誤,
二、包含了User作用域的配置項的時候,user.config檔案損壞了,
結合自己的程式確實用道<configSections>這個節點,用來保存程式在螢屏的位置,該節點已經在最上面,檢測內容格式也是正常,
那就剩下第二種可能了,找位置:C:\Users\Administrator\AppData\Local\SuspendWpfApp,洗掉里面的檔案即可,
Administrator:用戶名, SuspendWpfApp:軟體名,
為了徹底解決這個問題,不在使用 Properties.Settings.Default來保存資訊,自己用檔案來保存上次資訊,
因為一旦userSettings出現問題,軟體中依賴appSettings節點的資訊將都獲取不到,
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/1624.html
標籤:C#
