C#WPF为什么我的程序在其他计算机上崩溃?

时间:2020-06-30 13:51:38

标签: c# excel wpf crash

因此,我制作了一个可读取和写入excel文件的程序。

大约一年前,我做了一个类似的程序,但仍然可以正常工作。我已经积累了更多的经验,因此想制作一个新的版本。

新程序可以在每台计算机上正常运行,但是一旦我尝试单击我的按钮以检查excel文件并打开一个新窗口,该程序就会崩溃。

  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");
        }
    }

奇怪的是,这与我在程序的先前版本中使用的代码几乎相同,唯一的区别是,在新程序中实例化新窗口时,我传递了一些值。

我试图通过在代码的每一行之间显示一个消息框来查看问题,以查看崩溃之前到达的距离,并发现到达此行时它崩溃了,从而调试了该问题:window1.Show();

我相信可能是这样,但是使用excel处理也可能是这样。

.NET在开发人员PC和客户端PC上都是最新的。

任何人都知道这里发生了什么吗?任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

我发现了问题所在。这确实是一个业余错误。

在新窗口中,有一张图片。并且在代码中它将从我的计算机获取图片。难怪它在所有其他计算机上崩溃了。

我只是忘了在发布之前解决此问题。