IOException未处理.错误的文件名或号码 [英] IOException was unhandled. Bad file name or number

查看:89
本文介绍了IOException未处理.错误的文件名或号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写此代码是为了从文件中读取选定的索引并显示其详细信息,但是标题中出现了错误.

I wrote this to read a selected index from a file and display its details but I'm having the error stated in the title.

我的代码:

'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)时做"

the error appears in line 19; "Do While Not EOF(1)"

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

Any help will be appreciated. Thanks.

推荐答案

您说的是错误

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

因此,如果您有多个记录,那么在第二个循环中,您的代码将由于提到的错误而失败

So if you have more than one record, at the second loop your code fails with the mentioned error

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

Move the FleClose(1) call outside the loop when you have really finished to read your records....
Of course this leaves the problem how to handle with more than one record.

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

Finally, we are in VB.NET here, the old VB6 function should be dumped as soon as possible. So, unless you are just maintaining an old project, take some time to change this code to use

string.Substring
Int32.TryParse
StreamReader
使用声明

随时添加.....

这篇关于IOException未处理.错误的文件名或号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆