【问题标题】:IOException was unhandled. Bad file name or numberIOException 未处理。错误的文件名或编号
【发布时间】:2014-04-01 20:25:45
【问题描述】:

我写这篇文章是为了从文件中读取选定的索引并显示其详细信息,但我遇到了标题中所述的错误。

我的代码:

'Declare variables
Dim OrdersFile As String
Dim NumRecsFound As Integer
Dim orderString As String
Dim searchID As Integer

'Find the selected order ID
orderString = lstOrder.Text
searchID = Val(Microsoft.VisualBasic.Left(orderString, 10))

'Set up the path to the orders file
OrdersFile = Application.StartupPath & "/orders.dat"

'Open the orders file in random access mode
FileOpen(1, OrdersFile, OpenMode.Random, , , Len(OrderRec))
'For each record in the file
RecPos = 1
'Read in every record until the end of the file is reached
Do While Not EOF(1)
    FileGet(1, OrderRec, RecPos)

    'If the record matches selected order, display the order file
    If OrderRec.OrderID = searchID Then
        NumRecsFound = NumRecsFound + 1

        With OrderRec
            txtOrderID.Text = .OrderID
            txtOrderDate.Text = .OrderDate
            txtPrice.Text = .Price
            cmbCustomers.Text = .CustomerID
            cmbPizza.Text = .PizzaID
        End With
    End If

    'Lock fields until edit is selected
    txtOrderID.Enabled = False
    txtFee.Enabled = False
    txtOrderDate.Enabled = False
    txtStatus.Enabled = False
    txtPrice.Enabled = False
    cmbCustomers.Enabled = False
    cmbPizza.Enabled = False

    'Close the orders file and leave the subroutine
    FileClose(1)

    'If the records don't match then get the next record
    RecPos = RecPos + 1
Loop

'If no records are found then output message and close the file
If NumRecsFound = 0 Then
    MsgBox("Order " & searchID & " could not be found. Please try again.")
    FileClose(1)
End If
FileClose(1)

错误出现在第 19 行; “不做 EOF(1)”

任何帮助将不胜感激。谢谢。

【问题讨论】:

  • 这是一个与您的问题没有直接关系的提示:如果您在 VB.NET 中编程,为什么要使用这么多 VB6 方法(比 .NET 旧得多)?如果你习惯用 VB6 编程,你可能会依赖一些 VB6 方法,但你的代码几乎是 100% VB6!
  • @varocarbas 写的一样,一个就够了
  • @Steve 好的。谢谢你让我知道(每个人都在这里提供不相关的建议,没有人解决实际问题哈哈)。
  • 循环内的那个 FileClose ......嗯,没什么好说的。您的文件中有多少条记录?
  • @varocarbas 好的,谢谢。我是一名学生,有点遵循模板,但做一些我自己的事情。我的老师说我们在 vb.net 中编码。谢谢你告诉我。

标签: vb.net ioexception


【解决方案1】:

你说错误就行了

Do While Not EOF(1)
    FileGet(1, OrderRec, RecPos)
    .......

但你有这个

   'Close the orders file and leave the subroutine
    FileClose(1)

    'If the records don't match then get the next record
    RecPos = RecPos + 1
Loop

因此,如果您有多个记录,则在第二个循环中,您的代码将因上述错误而失败

当您真正完成读取记录时,将 FleClose(1) 调用移到循环之外....
当然,这留下了如何处理多条记录的问题。

最后,我们在VB.NET这里,旧的VB6功能应该尽快转储。因此,除非您只是维护一个旧项目,否则请花一些时间来更改此代码以使用

string.Substring
Int32.TryParse
StreamReader
Using Statement

欢迎添加.....

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-08
    • 2014-09-23
    • 1970-01-01
    • 2011-12-05
    • 2013-08-10
    • 2010-10-12
    • 2022-06-15
    • 1970-01-01
    相关资源
    最近更新 更多