我的代码无法从文本文件中读取

时间:2015-05-06 14:07:44

标签: vb.net

我的Visual Basic代码可以写入文件,但不能从中读取。从文件中读取需要成为启动画面的一部分,因此我无法将其移动到主KMFile类。你能帮我弄清楚这段代码有什么问题吗?

Imports System.IO

Public Class SplashScreen1

    'TODO: This form can easily be set as the splash screen for the application by going to the "Application" tab
    '  of the Project Designer ("Properties" under the "Project" menu).


    Private Sub SplashScreen1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Set up the dialog text at runtime according to the application's assembly information.
        'Open the yacht type list to read from it.

        'TODO: Customize the application's assembly information in the "Application" pane of the project 
        '  properties dialog (under the "Project" menu).

        'Application title
        Dim YachtTypeString As String = "YachtTypes.txt"

        Try
            'Open the file.
            Dim YachtsListStreamReader As StreamReader = New StreamReader("YachtTypes.txt")
            ' Read all elements into the list.
            Do Until YachtsListStreamReader.Peek = -1
                YachtTypeString = YachtsListStreamReader.ReadLine()
                KMFile.YachtTypeComboBox.Items.Add(YachtTypeString)
            Loop
            'Close the file.
            YachtsListStreamReader.Close()
        Catch ex As Exception
            'File missing.
            MessageBox.Show("The yacht types file is not found.", "Enter yacht types manually to save", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Try

        If My.Application.Info.Title <> "" Then
            ApplicationTitle.Text = My.Application.Info.Title
        Else
            'If the application title is missing, use the application name, without the extension
            ApplicationTitle.Text = System.IO.Path.GetFileNameWithoutExtension(My.Application.Info.AssemblyName)
        End If

        'Format the version information using the text set into the Version control at design time as the
        '  formatting string.  This allows for effective localization if desired.
        '  Build and revision information could be included by using the following code and changing the 
        '  Version control's designtime text to "Version {0}.{1:00}.{2}.{3}" or something similar.  See
        '  String.Format() in Help for more information.
        '
        '    Version.Text = System.String.Format(Version.Text, My.Application.Info.Version.Major, My.Application.Info.Version.Minor, My.Application.Info.Version.Build, My.Application.Info.Version.Revision)

        Version.Text = System.String.Format(Version.Text, My.Application.Info.Version.Major, My.Application.Info.Version.Minor)

        'Copyright info
        Copyright.Text = My.Application.Info.Copyright
    End Sub

End Class

1 个答案:

答案 0 :(得分:1)

下面:

Catch ex As Exception
    'File missing.
    MessageBox.Show("The yacht types file is not found.", "Enter yacht types manually to save", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Try

您吞下所有例外并显示一般错误消息。不要那样做。在显示的消息中包含ex.Message或仅 抓住FileNotFoundExceptions

这样,您将看到真实错误消息,从而引导您遇到问题的真实原因。