简体   繁体   中英

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

So I made a program that reads and writes to excel files.

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.

  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.

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();

I believe that could be the case, but it could also be something with the excel handling.

.NET is up to date on both developer pc and client pc.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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