VB.NET中的IndexOutOfRangeException

时间:2009-06-09 10:54:07

标签: vb.net .net-3.5

选择最初为.NET框架1.1版编写的旧版VB.NET项目。我用.NET 3.5运行Vista。我已经清除了所有原始错误,项目将构建;它只是不会运行。

据我所知,它正在尝试运行'LoginForm',但是输入断点不起作用,因为在到达断点之前抛出错误,无论文件放在何处。

真的无法解决该怎么办!任何帮助表示赞赏。

堆栈跟踪:

System.IndexOutOfRangeException was unhandled
  Message="Index was outside the bounds of the array."
  Source="FirstLine"
  StackTrace:
       at FirstLine.LoginForm.main()
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 
编辑:非常抱歉,不欣赏代码会有多大用处,因为问题更多是我无法理解的。但是,这是主要功能:

Shared Sub main()
        Dim p As Process() = Process.GetProcessesByName("FirstLine")
        If p.Length = 1 Then
            'START COPYMINDER
            'Dim expirydate As Date = CDate("01/01/1970")
            'Dim expiry As Integer
            'Try
            '    GetCopyMinderExpiryDate(expiry)
            '    If Not expiry = 0 And Not expiry = 1 Then
            '        expirydate = expirydate.AddSeconds(expiry)
            '        Dim diff As Integer = DateDiff(DateInterval.Day, Date.Now, expirydate)
            '        If diff >= 0 And diff  0 Then
            '    DisplayError((ret_code))
            '    End
            'End If

            'Dim did As String
            'GetCopyMinderDeveloperID(did)
            'If did  "IT" Then
            '    MessageBox.Show("Invalid Developer ID " & did & ". Firstline will now shutdown", "Firstline", MessageBoxButtons.OK, MessageBoxIcon.Error)
            '    End
            'End If

            'END COPYMINDER


            Dim lf As New LoginForm
            If LoginSettings.setting("loginShowErrorOnLine") = "TRUE" Then
                lf.ShowDialog()
            Else
                Try
                    lf.ShowDialog()
                Catch ex As Exception
                    MsgBox(ex.Message)
                    Config.UnlockByUser(Config.currentUser.username)
                    Config.currentUser.UserLoggedOff()
                End Try
            End If
        Else
            Dim prc As Process = p(0)
            SwitchToThisWindow(prc.MainWindowHandle, True)
        End If

    End Sub

感谢您的回复。看到这样一个有用的社区真是令人鼓舞!

2 个答案:

答案 0 :(得分:4)

Dim prc As Process = p(0)是你的问题,因为它在else语句中,数组长度可以是1(例如0)。

当长度为0时,当您尝试访问第一个元素时,它将为您提供IndexOutOfRange。

答案 1 :(得分:0)

尝试启用调试版本;这将为您提供添加断点的可能性,并且还会在堆栈跟踪中为您提供一些行号。

您的问题是您没有处理p.Length = 0的情况。如果没有名为“FirstLine”的进程,则会发生这种情况。

您是否也重命名过程/应用程序?