读取保存的文本文件的所有行 [英] Read saved text file all lines

查看:82
本文介绍了读取保存的文本文件的所有行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何阅读保存的文本文件新表格的所有行(例如:form3)



这是我的代码



how do i Read saved text file all lines of a new form (ex : form3 )

Here is my code

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Dim i As Integer = 0
    Dim newForm As Form3 = New Form3
    Dim filepath As String = IO.Path.Combine("D:\Logs", TextBox13.Text + i.ToString() + ".txt")
    Dim readText() As String = File.ReadAllLines(filepath)
    Dim s As String
    newForm.TextBox1.Text = File.ReadAllLines(filepath)
    For Each s In readText
        Console.WriteLine(s)
    Next
End Sub





我的尝试:



我试过上面的代码请给我一个修复



What I have tried:

I have tried the above code please provide me with a fix

推荐答案

猜猜你的表格是目前显示 System.String [] 而不是文件的内容。



那是因为 ReadAll行返回表示文件中行的字符串。 VB.NET帮助将其转换为与 TextBox1.Text - 一个 String 相同的类型 - 通过调用array的 ToString 方法。由于数组不会覆盖 ToString 方法,因此只返回类型名称。



显示内容而是使用 ReadAllText 方法:

At a guess, your form is currently displaying System.String[] instead of the content of the file.

That's because ReadAllLines returns an array of strings representing the lines in the file. VB.NET is "helpfully" converting that to the same type as TextBox1.Text - a String - by calling the array's ToString method. Since arrays don't override the ToString method, it simply returns the type name.

To display the contents of the file instead, use the ReadAllText method:
newForm.TextBox1.Text = File.ReadAllText(filepath)



注意:您还需要显示新表单,否则它永远不会出现在屏幕上。



为了避免这种问题,你应该打开 Option Strict ,这将阻止VB的有用自动类型强制。


NB: You'll also need to Show the new form, otherwise it will never appear on screen.

To avoid this kind of problem, you should turn on Option Strict, which will prevent VB's "helpful" automatic type coercion.


这篇关于读取保存的文本文件的所有行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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