我已经开始开发一个新的应用程序来从字符串生成md5哈希,它会自动保存到my.settings.md5_hashes 现在我需要检查我的设置md5_hashes中是否存在某个值。我找到了一些例子,但它给了我一些错误 错误 错误1'System.Collections.Specialized.StringCollection'类型的值无法转换为'String'。 如何检查我的设置中是否存在一个值?
这是我的代码
Public Class Form3
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If My.Settings.md5_hashes = (TextBox1.Text) Then
End If
If Not My.Settings.md5_hashes = (TextBox1.Text) Then
Me.Show()
End If
End Sub
End Class
这是生成哈希值并将其保存到my.settings.md5_hashes的代码
Imports System.Text
Imports System.Security.Cryptography
Imports System
Imports System.Collections
Imports System.Collections.Specialized
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
My.Settings.md5_hashes.Clear()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim md5 As MD5 = System.Security.Cryptography.MD5.Create()
Dim inputBytes As Byte() = System.Text.Encoding.ASCII.GetBytes(TextBox1.Text)
Dim hash As Byte() = md5.ComputeHash(inputBytes)
Dim sb As New StringBuilder()
For i As Integer = 0 To hash.Length - 1
sb.Append(hash(i).ToString("x2"))
Next
TextBox2.Text = sb.ToString
ListBox1.Items.Add(TextBox1.Text + "<--->" + TextBox2.Text)
My.Settings.md5_hashes.Add(TextBox1.Text + "<--->" + TextBox2.Text)
My.Settings.Save()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
For Each item In My.Settings.md5_hashes
ListBox1.Items.Add(item)
Next
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
TextBox1.Text = ""
TextBox2.Text = ""
ListBox1.Items.Clear()
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
Form2.Show()
End Sub
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
Dim W As IO.StreamWriter
Dim i As Integer
W = New IO.StreamWriter("C:\MD5.txt", True)
For i = 0 To ListBox1.Items.Count - 1
W.WriteLine(ListBox1.Items.Item(i))
Next
W.Close()
MsgBox("You File Is Save You Can Locate It At C:\MD5.txt")
End Sub
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
Form3.Show()
End Sub
End Class
我刚刚更改了部分代码 这是我的新代码
Imports System.Text
Imports System.Security.Cryptography
Imports System
Imports System.Collections
Imports System.Collections.Specialized
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'My.Settings.md5_hashes.Clear()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim md5 As MD5 = System.Security.Cryptography.MD5.Create()
Dim inputBytes As Byte() = System.Text.Encoding.ASCII.GetBytes(TextBox1.Text)
Dim hash As Byte() = md5.ComputeHash(inputBytes)
Dim sb As New StringBuilder()
For i As Integer = 0 To hash.Length - 1
sb.Append(hash(i).ToString("x2"))
Next
TextBox2.Text = sb.ToString
ListBox1.Items.Add(TextBox1.Text)
ListBox1.Items.Add(TextBox2.Text)
My.Settings.md5_hashes.Add(TextBox1.Text)
My.Settings.md5_hashes.Add(TextBox2.Text)
My.Settings.md5_hashes.Add("<--->")
My.Settings.Save()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
For Each item In My.Settings.md5_hashes
ListBox1.Items.Add(item)
Next
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
TextBox1.Text = ""
TextBox2.Text = ""
ListBox1.Items.Clear()
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
Form2.Show()
End Sub
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
Dim W As IO.StreamWriter
Dim i As Integer
W = New IO.StreamWriter("C:\MD5.txt", True)
For i = 0 To ListBox1.Items.Count - 1
W.WriteLine(ListBox1.Items.Item(i))
Next
W.Close()
MsgBox("You File Is Save You Can Locate It At C:\MD5.txt")
End Sub
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
Form3.Show()
End Sub
End Class
现在它的返回是真的,但我如何才能获得在msgbox上显示结果的值? 好吧,我解决了我自己获取名称结果的方式,例如,如果我用名称搜索检索哈希,但如果我搜索哈希并得到名称如何取名称我存储在那里的原因怎么办?
答案 0 :(得分:0)
If My.Settings.md5_hashes.Contains(TextBox1.Text) = True Then
'Hash exists, do something.
Else
'Hash does not exist, do something else.
End If
提示:检查当您键入My.Settings.md5_hashes.
时,IntelliSense会向您显示的内容(注意结尾处的点)。通过这样做,你很可能自己找到答案。
修改强>
在您的特定情况下,它会变得更复杂,因为您必须先分离输入和哈希,然后才能检查哈希是否存在。
试试这个(这是你在问题中发布的第一个代码):
Private Function HashExists(ByVal Hash As String) As Boolean
For Each Entry As String In My.Settings.md5_hashes 'Iterate through every hash entry.
Dim Parts As String() = Entry.Split(New String() {"<--->"}, 2, StringSplitOptions.RemoveEmptyEntries) 'Split the entry on "<--->".
If Parts.Length = 1 AndAlso Parts(0) = Hash Then
Return True 'No "<--->" was present, but the entire entry matches 'Hash'.
ElseIf Parts.Length >= 2 AndAlso Parts(1) = Hash
Return True 'The entry was split successfully and the second part and 'Hash' are equal.
End If
Next
Return False 'No valid hash found.
End Function
然后使用它:
If HashExists(TextBox1.Text) = True Then
'Hash exists, do something.
Else
'Hash does not exist, do something else.
End If
编辑2:
要从名称中获取哈希值或哈希值的名称,只需稍微修改上述代码即可返回这两个部分中的任何一个。
Private Function GetEntry(ByVal Input As String, ByVal NameFromHash As Boolean) As String
For Each Entry As String In My.Settings.md5_hashes 'Iterate through every hash entry.
'Parts(0) is the name.
'Parts(1) is the hash.
Dim Parts As String() = Entry.Split(New String() {"<--->"}, 2, StringSplitOptions.RemoveEmptyEntries) 'Split the entry on "<--->".
If Parts.Length >= 2 Then
If NameFromHash = True AndAlso Parts(1) = Input Then
Return Parts(0) 'Input was a valid hash, return name.
ElseIf NameFromHash = False AndAlso Parts(0) = Input Then
Return Parts(1) 'Input was a valid name, return hash.
End If
End If
Next
Return Nothing 'No valid entry found.
End Function
用法:
'Get the name from a hash.
Dim Name As String = GetEntry(TextBox1.Text, True)
'Get the hash from a name.
Dim Hash As String = GetEntry(TextBox1.Text, False)
如果函数返回Nothing
,则表示找不到您指定的名称/哈希值。为了避免获得NullReferenceException
,您可以在If
语句中进行检查:
'Name from hash.
If Name IsNot Nothing Then
'The specified hash was found, do something with the name.
Else
'Hash not found.
MessageBox.Show("The specified hash was not found.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
'Hash from name.
If Hash IsNot Nothing Then
'The specified name was found, do something with the hash.
Else
'Name not found.
MessageBox.Show("The specified name was not found.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If