[英]my C# windows application runs on system startup. but works in windows xp and not in windows 7. I have written these codes
[英]My application works on my PC (Windows 7) but not another (XP) and I am not sure how to continue diagnosing the issue
問題背景
我已經在我的PC(使用Visual Studio 2010 Express的Windows 7)上開發數據庫應用程序,並且該數據庫應用程序已按預期工作。 我只是將最新版本復制到運行Windows XP的PC上,並拋出"Program has encountered an error and needs to close"
錯誤消息。
應用程序事件日志中錯誤消息中的信息表明,錯誤發生在initializeDatabaseConnection()
(根據堆棧跟蹤)-這是一種相當簡短的方法,僅包含幾行代碼。
dbConnection = new MySql.Data.MySqlClient.MySqlConnection("server=myserver;
uid=myuser;
pwd=mypassword;
database=mydatabase;");
dbCommand = new MySql.Data.MySqlClient.MySqlCommand();
dbCommand.Connection = dbConnection;
嘗試診斷問題所采取的步驟
我添加了一個try-catch
在代碼中initializeDatabaseConnection()
來捕捉任何Exception
,並調用MessageBox.Show(ex.ToString()
-相同的錯誤發生之前,並沒有顯示消息框,它說,有未處理例外,盡管在堆棧跟蹤中顯示了此方法。
我在catch
塊中添加了一個更具體的處理程序,一個用於System.IO.FileLoadException
處理程序,這是事件日志條目描述的特定錯誤消息。 發生與上述通用Exception處理程序相同的行為。
我在Stack Overflow上發現了一個問題,該問題描述了一個應用程序在移至另一台計算機上之前按預期運行的問題,這提示了Visual Studio遠程調試-但是,由於我根據文檔使用了Visual Studio 2010 Express,因此我無法使用此功能。
關於堆棧溢出的類似問題建議,第一步,應檢查應用程序針對的.NET Framework
版本不高於其他PC可以處理的版本。 我將其關閉至3.5
,仍然出現相同的錯誤,並且我相信另一台PC仍可以運行許多4.0
應用程序。
相同的問題建議您在“ Build Settings
檢查要構建的平台,以確認將其設置為x86
我已經這樣做了。
我按照Aybe的建議添加了未處理的異常事件處理程序,該處理程序表明問題是Could not load file or assembly, MySql.Data ... etc.
。 但是,我不確定為什么此錯誤會突然發生,就像以前一樣版本的版本按預期工作。
解
MySql.Data.dll
版本為6.8.3.0
但是文件夾中的版本為6.9.3.0
我不確定為什么現在需要6.8.3.0
因為客戶端PC上應用程序文件夾中的版本為尚未更改,並且6.9.3.0
正在使用以前的版本。 我發現這歸功於下面的注釋,該注釋建議我實現UnhandledExceptionEventHandler
並鏈接到文檔以了解如何執行此操作。 這是我以前不知道的CLR的功能。 謝謝!
雖然我仍然不確定為什么未處理異常(例如,根據應用程序事件日志條目的堆棧跟蹤,該異常發生在一種方法中,其中所有代碼均由通用Exception catch
-block封裝),但我能夠實際上,通過使用UnhandledExceptionEventHandler
捕獲未處理的Exception。
public static void Main()
{
AppDomain.CurrentDomain.UnhandledException
+= new UnhandledExceptionEventHandler(MyHandler);
}
static void MyHandler(object sender, UnhandledExceptionEventArgs args)
{
Exception e = (Exception) args.ExceptionObject;
// Do what you like with the exception
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.