[英]C# WPF Why Does my program crash on other computers?
所以我做了一個程序來讀寫 excel 文件。
大約一年前,我做了一個類似的程序,它仍然可以正常工作。 我做了一個新的,因為我獲得了更多的經驗,並且想要一個更新的版本。
新程序在每台計算機上都可以正常啟動,但是一旦我嘗試單擊按鈕檢查 excel 文件並打開新的 window,程序就會崩潰。
private void Button_Click(object sender, RoutedEventArgs e)
{
string serial = txt_ser.Text;
if (!string.IsNullOrEmpty(serial))
{
Filesearch(serial); // Searches the directories and sets Common.Filename as the found filepath
if (!string.IsNullOrEmpty(Common.Filename) || !string.IsNullOrWhiteSpace(Common.Filename))
{
Microsoft.Office.Interop.Excel.Application follo;
Microsoft.Office.Interop.Excel.Workbook xlWB;
Microsoft.Office.Interop.Excel.Worksheet xlWS;
object misvalue = System.Reflection.Missing.Value;
follo = new Microsoft.Office.Interop.Excel.Application();
follo.Visible = false;
try
{
xlWB = follo.Workbooks.Open(Common.Filename);
Microsoft.Office.Interop.Excel.Sheets excelSheets = xlWB.Worksheets;
string currentSheet = "Main";
xlWS = (Microsoft.Office.Interop.Excel.Worksheet)excelSheets.get_Item(currentSheet);
//Read a bunch of excel cells and place their values in an ObservableCollection called partslist here.
xlWB.Close();
follo.Quit();
Window1 window1 = new Window1(serial, Common.Filename, variant, partslist );
window1.Show();
this.Close();
}
catch (Exception ex)
{
follo.Quit();
MessageBox.Show(ex.Message);
}
}
else
{
MessageBox.Show("Could not find " + serial + ". Is :N available on this computer?");
}
}
else
{
MessageBox.Show("Please enter a serial number");
}
}
奇怪的是,這與我在之前版本的程序中使用的代碼幾乎相同,唯一的區別是我在新程序中實例化新的 window 時傳遞了一些值。
我試圖通過在每行代碼之間顯示一個消息框來調試問題,以查看它在崩潰之前有多遠,我發現它在到達這一行時崩潰:window1.Show();
我相信可能是這種情況,但它也可能與 excel 處理有關。
.NET 在開發人員電腦和客戶端電腦上都是最新的。
有人知道這里發生了什么嗎? 任何幫助將不勝感激。
我發現了問題所在。 這真是一個業余錯誤。
在新的window中有一張圖。 在代碼中,它會從我的電腦中獲取圖片。 難怪它在所有其他計算機上都崩潰了。
我只是忘記在發布之前修復它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.