将文本文件中的每一行与 Text/Combo 框中的文本进行比较
Compare each line in a text file with text in Text/Combo box
我需要能够将文本文件的每一行与用户在文本或组合框中键入的文本进行比较,以检查序列号并阻止某些网站。目前,被阻止的内容在程序代码本身内。
If ComboBox1.Text = "website.com" Then
WB.DocumentText = My.Settings.BlockedPage
ComboBox1.Text = "Blocked"
Else
WB.Navigate(Me.ComboBox1.Text)
ComboBox1.Items.Add(ComboBox1.Text)
End If
我发现这种方式很烦人,因为它会增加已编译可执行文件的文件大小,并阻止我在不发布全新可执行文件的情况下更新它。
我在另一个程序中使用过这种方法,但我确信我使用的代码会将它与整个文本文件而不是一行进行比较。
Dim key As String
key = My.Computer.FileSystem.ReadAllText("key.txt")
If TextBox1.Text = key Then
'Do functions, such as create licence file
Me.Close()
Else
MsgBox("Incorrect key", MsgBoxStyle.Exclamation, "Key Check")
End If
我是否可以修改该代码以允许我做我想做的事?
谢谢
这是一种方法:
Dim lines = System.IO.File.ReadAllLines("File path")
Dim text As String
For i = 0 To lines.Length - 1
text = lines(i).ToString
If text = "Your serial here" Then
MsgBox("Serial found")
else
Msgbox("Serial not found")
End If
Next
您可以将此代码用于网络浏览器:
Dim lines = System.IO.File.ReadAllLines(Application.StartupPath + "file path")
Dim text As String
For i = 0 To lines.Length - 1
text = lines(i).ToString
If WebBrowser1.Url.ToString.Contains(text) Then
WebBrowser1.Navigate("")
End If
Next
文本文件中的文本应采用以下格式:
www.website.com
尝试
If ComboBox1.Text.Equals("website.com") Then
WB.DocumentText = My.Settings.BlockedPage
ComboBox1.Text = "Blocked"
Else
WB.Navigate(Me.ComboBox1.Text)
ComboBox1.Items.Add(ComboBox1.Text)
End If
或
If ComboBox1.Text.Contains("website.com") Then
WB.DocumentText = My.Settings.BlockedPage
ComboBox1.Text = "Blocked"
Else
WB.Navigate(Me.ComboBox1.Text)
ComboBox1.Items.Add(ComboBox1.Text)
End If
使用 .Equal() 或 .Contains()。当您尝试与 .txt 文件进行比较时,请尝试使用 .contain()
我需要能够将文本文件的每一行与用户在文本或组合框中键入的文本进行比较,以检查序列号并阻止某些网站。目前,被阻止的内容在程序代码本身内。
If ComboBox1.Text = "website.com" Then
WB.DocumentText = My.Settings.BlockedPage
ComboBox1.Text = "Blocked"
Else
WB.Navigate(Me.ComboBox1.Text)
ComboBox1.Items.Add(ComboBox1.Text)
End If
我发现这种方式很烦人,因为它会增加已编译可执行文件的文件大小,并阻止我在不发布全新可执行文件的情况下更新它。
我在另一个程序中使用过这种方法,但我确信我使用的代码会将它与整个文本文件而不是一行进行比较。
Dim key As String
key = My.Computer.FileSystem.ReadAllText("key.txt")
If TextBox1.Text = key Then
'Do functions, such as create licence file
Me.Close()
Else
MsgBox("Incorrect key", MsgBoxStyle.Exclamation, "Key Check")
End If
我是否可以修改该代码以允许我做我想做的事? 谢谢
这是一种方法:
Dim lines = System.IO.File.ReadAllLines("File path")
Dim text As String
For i = 0 To lines.Length - 1
text = lines(i).ToString
If text = "Your serial here" Then
MsgBox("Serial found")
else
Msgbox("Serial not found")
End If
Next
您可以将此代码用于网络浏览器:
Dim lines = System.IO.File.ReadAllLines(Application.StartupPath + "file path")
Dim text As String
For i = 0 To lines.Length - 1
text = lines(i).ToString
If WebBrowser1.Url.ToString.Contains(text) Then
WebBrowser1.Navigate("")
End If
Next
文本文件中的文本应采用以下格式:
www.website.com
尝试
If ComboBox1.Text.Equals("website.com") Then
WB.DocumentText = My.Settings.BlockedPage
ComboBox1.Text = "Blocked"
Else
WB.Navigate(Me.ComboBox1.Text)
ComboBox1.Items.Add(ComboBox1.Text)
End If
或
If ComboBox1.Text.Contains("website.com") Then
WB.DocumentText = My.Settings.BlockedPage
ComboBox1.Text = "Blocked"
Else
WB.Navigate(Me.ComboBox1.Text)
ComboBox1.Items.Add(ComboBox1.Text)
End If
使用 .Equal() 或 .Contains()。当您尝试与 .txt 文件进行比较时,请尝试使用 .contain()