简体   繁体   English

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

[英]C# WPF Why Does my program crash on other computers?

So I made a program that reads and writes to excel files.所以我做了一个程序来读写 excel 文件。

About one year ago I made a similar program that still works fine.大约一年前,我做了一个类似的程序,它仍然可以正常工作。 I made a new one as I have gained more experience, and wanted an updated version.我做了一个新的,因为我获得了更多的经验,并且想要一个更新的版本。

The new program launches just fine on every computer, but once I try to click my button to check an excel file and open a new window, the program crashes.新程序在每台计算机上都可以正常启动,但是一旦我尝试单击按钮检查 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");
        }
    }

The weird thing is that this is pretty much the same code I used in the previous version of my program, the only difference is that I pass some values when instantiating the new window in my new program.奇怪的是,这与我在之前版本的程序中使用的代码几乎相同,唯一的区别是我在新程序中实例化新的 window 时传递了一些值。

I tried to debug the issue by showing a messagebox between each line of code to see how far it gets before crashing, and what I found is that it crashes when it reaches this line: window1.Show();我试图通过在每行代码之间显示一个消息框来调试问题,以查看它在崩溃之前有多远,我发现它在到达这一行时崩溃:window1.Show();

I believe that could be the case, but it could also be something with the excel handling.我相信可能是这种情况,但它也可能与 excel 处理有关。

.NET is up to date on both developer pc and client pc. .NET 在开发人员电脑和客户端电脑上都是最新的。

Anyone have any idea what's happening here?有人知道这里发生了什么吗? Any help would be very much appreciated.任何帮助将不胜感激。

I found out what the issue was.我发现了问题所在。 It was really an amateur error.这真是一个业余错误。

In the new window, there is a picture.在新的window中有一张图。 And in the code it would get the picture from my computer.在代码中,它会从我的电脑中获取图片。 No wonder it crashed on all other computers.难怪它在所有其他计算机上都崩溃了。

I simply forgot to fix that before publishing.我只是忘记在发布之前修复它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM