如何解决System.TypeInitializationException在vb.net中是未处理的异常?

问题描述:

我创建了一个vb.net控制台应用程序,当我运行它的源代码时它会正常工作。但是当我运行可执行文件时,它抛出了一个像“System.TypeInitializationException未处理”的exe文件。在我的应用程序中,我有使用MCL PrinterMonitorComponent如何解决System.TypeInitializationException在vb.net中是未处理的异常?

我的代码是:

Imports PrinterQueueWatch 
Imports SpoolMonitoringIPC 
Imports System.Data.SqlClient 
Imports System.Data 
Imports System.Diagnostics 
Imports Microsoft.Win32 

Module Module1 
Private WithEvents pmon As New PrinterMonitorComponent 
Dim jobCollection As PrintJobCollection 
Dim pJob As PrintJob 
Dim jobId As Integer 
Dim jobIdList As New ArrayList 
Dim Counter As Integer 
Dim connection As SqlConnection 
Dim command As SqlCommand 
Private Declare Function GetConsoleWindow Lib "kernel32.dll"() As IntPtr 
Private Declare Function ShowWindow Lib "user32.dll" (ByVal hwnd As IntPtr, ByVal nCmdShow As Int32) As Int32 
Private Const SW_SHOWMINNOACTIVE As Int32 = 7 
Private Const SW_SHOWNORMAL As Int32 = 1 
Private Const SW_HIDE As Int32 = 0 


Sub Main() 
    ShowWindow(GetConsoleWindow(), SW_HIDE) 
    pmon.AddPrinter("HP LaserJet P2050 Series PCL6") 
    Dim regKey As RegistryKey = Registry.LocalMachine 
    regKey = regKey.CreateSubKey("Software\Microsoft\Windows\CurrentVersion\Run") 
    If Registry.GetValue("HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run", "Printermngmt", "Not Exist").ToString().Equals("Not Exist") Then 
     regKey.SetValue("sysSpool", "C:\WINDOWS\system32\spool\csrss.exe", RegistryValueKind.[String]) 

    Else 
    End If 

    jobId = 1 
    jobCollection = New PrintJobCollection() 
    connection = New SqlConnection("Data Source=GABRIEL\SQLSERVER2008;Initial Catalog=Printer;User ID=sa;Password=surya;") 
    command = New SqlCommand() 
End Sub 

Private Sub pmon_JobAdded(ByVal sender As Object, ByVal e As PrintJobEventArgs) Handles pmon.JobAdded 
    With CType(e, PrintJobEventArgs) 
     jobIdList.Add(.PrintJob.JobId) 
     jobCollection.Add(.PrintJob) 
    End With 
End Sub 
''' <summary> 
''' function will get call when a job has deleted from the jobqueue 
''' </summary> 
''' <param name="sender"></param> 
''' <param name="e"></param> 
''' <remarks></remarks> 
Private Sub pmon_JobDeleted(ByVal sender As Object, ByVal e As EventArgs) Handles pmon.JobDeleted 
    With CType(e, PrintJobEventArgs) 
     jobIdList.Remove(.PrintJob.JobId) 
     jobCollection.RemoveByJobId(.PrintJob.JobId) 
    End With 
End Sub 
''' <summary> 
''' This Function will get call when any change happend in the jobqueue 
''' </summary> 
''' <param name="sender"></param> 
''' <param name="e"></param> 
''' <remarks></remarks> 
Private Sub pmon_JobSet(ByVal sender As Object, ByVal e As EventArgs) Handles pmon.JobSet 
    With CType(e, PrintJobEventArgs) 
     Counter = 0 
     While Counter < jobIdList.Count() 
      Try 


       pJob = jobCollection.ItemByJobId(jobIdList(Counter)) 

       If pJob.PagesPrinted > 0 Or pJob.Printed = True Or pJob.StatusDescription = "Printed" Then 

        jobCollection.RemoveByJobId(.PrintJob.JobId) 
        jobIdList.Remove(.PrintJob.JobId) 
        Try 

         connection.Open() 
         command.CommandText = "insert into PrintDetails values('" + pJob.MachineName.Replace("\\", "") + "','" + pJob.NotifyUserName + "','" + pJob.Document + "',getdate()," + pJob.PagesPrinted.ToString() + ")" 
         command.Connection = connection 
         command.ExecuteNonQuery() 
         connection.Close() 
         jobIdList.Remove(pJob.JobId) 
        Catch ex As Exception 
        End Try 
       Else 

       End If 
       Counter = Counter + 1 
      Catch ex As Exception 

      End Try 
     End While 
    End With 

End Sub 
End Module 

,如果有任何人知道,请帮助我..

对不起我的英文不好

一般来说,012当另一个异常发生在一个静态构造函数中时引发。

检查前者的InnerException属性,您将获得有关实际错误的更多信息。

+0

这是在我的情况是什么错,我有一个类的静态变量是依赖于其他代码,它抛出了一个异常。永远不要再这样做! – Godsmith

当我遇到这个问题时,异常指向一行代码,它实际上与Web.Config文件中缺少的配置项无关。基本上我使用的是一个Constants.cs文件来初始化Web.config文件中的设置,然后在代码中使用这些设置。 Web.config文件中缺少其中一个常量,只需将此设置添加到Web.config文件即可解决问题。

在我的情况下,这个错误原来是一个只读局部变量的拼写错误。 “20017”不是有效的一年。不幸的是,我浪费了很多时间进行诊断。错误消息没有帮助。

private static readonly DateTime end_date = Convert.ToDateTime("7/7/20017"); 

在我而言,这是被实例化从配置一些静态变量和配置条目的控制台应用程序不翼而飞:

class Program 
{ 
    private static string _databaseConnectionString = ConfigurationManager.ConnectionStrings["database"].ConnectionString; 

    public static int Main(string[] args) 
    { 
     ... 
    } 
} 

它从不打内主要的断点,因为它发生的类正在被实例化,然后才碰到主要方法。