Winform程序安装后打开出现程序停止运行问题解决方法

我用的是VS2013版本,打包的是Winform应用程序。

一开始以为是打包过程有问题,结果试很多次。每次安装成功后,一打开程序,就出现程序停止运行。如下图所示。

Winform程序安装后打开出现程序停止运行问题解决方法

调了一晚上都没有头绪,果断放弃。走进了死胡同,钻牛角尖只会是让自己越来越痛苦,不如休息一下,换换脑子,理理思路。

第二天虽然是周六,但是不甘心,加班找问题。在百度里面查资料,希望能有一些启发,果然还是有帮助的。

解决办法:

1.出现以上问题的主要原因是,主程序中的创建数据库错误,具体一点就是数据库路径错误。配置文件app.config中有一处数据库连接路径为本机的绝对路径,后来改成如下

<connectionStrings>
    <!--  数据库为相对路径 在根目录下的App_Data文件夹下-->
    <add name="GPSAnalyzer.Properties.Settings.gpsConnectionString"
      connectionString="Data Source=|DataDirectory|gps.db" providerName="System.Data.SQLite.EF6" />
  </connectionStrings>

2.如下是获取数据库路径代码,可以写在操作数据库的类中,如***Helper,或者单独写在Common类中:

 public static string getDBPath()
        {
            //数据库绝对路径
            string dbPath = AppDomain.CurrentDomain.BaseDirectory;
            if (dbPath.IndexOf("\\bin\\") > 0)
            {
                if (dbPath.EndsWith("\\bin\\Debug\\"))
                    dbPath = dbPath.Replace("\\bin\\Debug", "");
                if (dbPath.EndsWith("\\bin\\Release\\"))
                    dbPath = dbPath.Replace("\\bin\\Release", "");
            }
            if (!dbPath.EndsWith("App_Data\\"))
                dbPath = dbPath + "App_Data\\";
            AppDomain.CurrentDomain.SetData("DataDirectory", dbPath);


            string sqliteConn_temp = ConfigurationManager.AppSettings["sqliteConn"];
            string[] temp = sqliteConn_temp.Split('|');
            string db_filename = temp[2].Split(';')[0];
            dbPath = dbPath + db_filename;


            return dbPath;
        }

3.为什么能发现这个问题,是因为后来在程序中增加了错误日志。这个很重要!!!!!!方便调试!!!!!

由于没想到程序在一运行时就出Bug了,所以在主程序中并没有捕获异常,写错误日志,这是最大的失误。为了找出以上问题,我在主程序中增加了以下捕获异常的代码:

 static void Main()
        {
            #region 异常 程序停止运行
            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);// UnhandledException事件来处理非 UI 线程异常
            //添加非UI上的异常.
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);


            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            #endregion

          //以下是创建数据库、程序入口等代码

    }

private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            try
            {
                Exception ex = (Exception)e.ExceptionObject;


                Utility.Log.WriteSysLog("Program," + MethodBase.GetCurrentMethod().Name +" 错误:"+ ex.Message + "\n\nStack Trace:\n" + ex.StackTrace, ex);          
                
            }
            catch (Exception exc)
            {
                try
                {
                    MessageBox.Show(" Error",
                        " Could not write the error to the log. Reason: "
                        + exc.Message, MessageBoxButtons.OK, MessageBoxIcon.Stop);
                }
                finally
                {
                    Application.Exit();
                }
            }
        }


再次运行安装好的程序,程序依然停止运行。不过没关系,此时去查看安装目录下的log错误日志文件,就能发现错误信息啦。接下来就是根据错误信息修改程序啦,这个就各有各的区别了。