Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在VB.net中显示.txt文件的内容并进行比较_Vb.net_Compare - Fatal编程技术网

如何在VB.net中显示.txt文件的内容并进行比较

如何在VB.net中显示.txt文件的内容并进行比较,vb.net,compare,Vb.net,Compare,我有四个.txt文件,其中包含一个整数值,我想在VB.net中的不同框中显示这些整数中的每一个(每个框包含一个.txt文件中的值)。此外,我还想比较这些结果,并根据最高值执行其中一个命令串行端口1.Write(“1/”) SerialPort1.Write(“2/”) SerialPort1.Write(“3/”) SerialPort1.Write(“4/”)谢谢最好将数据存储在mysql之类的数据库中。并查询所需的数据 但是,您可以将文本加载到字符串中,然后将其解析为整数 Dim lstVa

我有四个.txt文件,其中包含一个整数值,我想在VB.net中的不同框中显示这些整数中的每一个(每个框包含一个.txt文件中的值)。此外,我还想比较这些结果,并根据最高值执行其中一个命令<代码>串行端口1.Write(“1/”) SerialPort1.Write(“2/”) SerialPort1.Write(“3/”)
SerialPort1.Write(“4/”)谢谢

最好将数据存储在mysql之类的数据库中。并查询所需的数据

但是,您可以将文本加载到字符串中,然后将其解析为整数

Dim lstValues as list(of Integer)
Dim file1 As String
Dim intGNum as integer
file1 = My.Computer.FileSystem.ReadAllText("C:\test.txt") 'copy same for other files

lstValue.add(integer.parse(file1))'duplicate for other files
'lstValue.add(integer.parse(file2)) 
'lstValue.add(integer.parse(file3))
'lstValue.add(integer.parse(file4))

intGNum=lstValue.Max();

首先,您需要首先从*.txt文件中获取所有文本数据,这可以通过使用轻松完成

接下来,您需要确保接收到的数据是有效的整数,这可以通过使用来轻松完成

最后,您需要获得最高值,因为它们都是数值,只需使用该方法即可。下面是一个简单的例子:

'Get all of the text from the desired file and assign it to the respective TextBox
TextBox1.Text = IO.File.ReadAllText("file1.txt")
TextBox2.Text = IO.File.ReadAllText("file2.txt")
TextBox3.Text = IO.File.ReadAllText("file3.txt")
TextBox4.Text = IO.File.ReadAllText("file4.txt")

'Check which TextBox has the highest value by first check if all are valid Integer values
Dim tb1, tb2, tb3, tb4 As Integer
If Integer.TryParse(TextBox1.Text, tb1) AndAlso
   Integer.TryParse(TextBox2.Text, tb2) AndAlso
   Integer.TryParse(TextBox3.Text, tb3) AndAlso
   Integer.TryParse(TextBox4.Text, tb4) Then

    'Get the highest value
    Dim highest As Integer = {tb1, tb2, tb3, tb4}.Max()

    'Do your command based on the highest value
    If ... Then

    End If
Else
    MessageBox.Show("One or more of the text files contain a value that cannot be converted into an Integer. Please check the files and try again.", "Invalid Data", MessageBoxButtons.OK)
End If

谢谢,但当我使用它时,我得到了一个错误:“lstValue”没有声明。由于其保护级别,它可能无法访问。抱歉,请在“列表(整数)”之前添加新内容。David的答案包含处理非整数所需的完整代码。我的答案是假设所有文本都带有整数。但是,使用多个文本文件来表示单个整数比在数据库表中显示这些信息要慢。问题是我真的不知道数据库是如何工作的,也不知道如何在其中实现我的数据。Youtube-如何安装本地database System.IO.File.ReadAllText(“C:\Users\rN.txt”)System.IO.File.ReadAllText(“C:\Users\rS.txt”)如果Integer.TryParse(“rN.Txt”,tb1)和同样的Integer.TryParse(“rS.Txt”,tb4),则Dim tb1、tb2、tb3、tb4为整数;如果tb1>tb4,则result=matlab.Execute(“cd C:\Users\Bo$\Documents\matlab\”)结果=matlab.Execute(“北”)(但我的结果是:一个或多个文本文件包含无法转换为整数的值。请检查这些文件)知道我的值是2.4935633e+02,谢谢