Save text on my.settings saved multiple times vb.net - vb.net

i have start a new application to encrypt strings and save them on my settings the code its work fine but wen it save it save multiple times the same.
How can i do for save all the content from a listbox to my settings with out to repeat.
This is my code
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
For Each item In My.Settings.md5_hashes
ListBox1.Items.Add(item)
Next
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
TextBox1.Text = ""
TextBox2.Text = ""
End Sub
End Class

Well i just solved my self the problem its because wen i start the timer its is there the code to retrieved all the content from my settings to the listbox.
I just removed and solved.

Related

Visual Basic won't change regkey value

So, I've been working on my program for 3 hours now (I'm new to VB),
so I've made the program to change the desktop wallpaper when a button is clicked, and it works fine, I want that as soon as you run the program as an administrator, that it changes the OEMBackground value is changed from 0 to 1,
but it won't work, why?
Imports System.Environment
Imports Microsoft.Win32
Imports System
Imports System.Reflection
Imports System.Runtime.InteropServices
Public Class Form_Main
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Me.Close()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
My.Computer.Audio.Play(My.Resources.Theme, AudioPlayMode.BackgroundLoop)
Dim ChangeValue As Integer
ChangeValue = 1
My.Computer.Registry.SetValue("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI\Background", "OEMBackground", ChangeValue)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Filepath As String
Filepath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\Microsoft\Windows\Themes\TranscodedWallpaper.jpg"
My.Resources.Monika.Save("TranscodedWallpaper.jpg", Imaging.ImageFormat.Jpeg)
If My.Computer.FileSystem.FileExists(Filepath) Then
My.Computer.FileSystem.DeleteFile(Filepath)
End If
My.Computer.FileSystem.MoveFile("TranscodedWallpaper.jpg", Filepath)
End Sub
End Class

How to determine if a value of a key exist in my settings vb.net

i have start to develop a new application to generate md5 hashes from strings and the it save automatic to my.settings.md5_hashes
and now i need to check if a certain value all ready exist in my settings md5_hashes. i have find some examples but it give me all ways a error on it
the error
Error 1 Value of type 'System.Collections.Specialized.StringCollection' cannot be converted to 'String'.
how can i check if a value its all ready exist on my settings?
This is my code
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
This is the code to generate the hashes and save it to 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
well i have just change some part of the code
this is my new code
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
well now its return true but how can i get the value to show on a msgbox the results?
well i solved my self the way to get results of the name for example if i search with the name to retrieved the hash , but how to do if i search for a hash and get the name how to take the name i stored initial there ?
Use the Contains method:
If My.Settings.md5_hashes.Contains(TextBox1.Text) = True Then
'Hash exists, do something.
Else
'Hash does not exist, do something else.
End If
Tip: Check what members IntelliSense shows you when you type My.Settings.md5_hashes. (notice the dot on the end). By doing so you would most likely have found the answer to this yourself.
EDIT:
In your specific case it gets a little more complicated because you must separate the input and the hash before you can check that the hash exists.
Try this (this is for the first code you posted in the question):
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
Then use this like:
If HashExists(TextBox1.Text) = True Then
'Hash exists, do something.
Else
'Hash does not exist, do something else.
End If
EDIT 2:
To get the name from a hash or a hash from a name the above code just need to be modified slightly to return any of the two parts.
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
Usage:
'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)
If the function returns Nothing that means it couldn't find the name/hash you specified. To avoid getting a NullReferenceException you can check so in an If-statement:
'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

How do get only available ssid with visual basic 2010

Sorry for bad english
I want the available ssid list in listbox
I am use api and its work but It stopped working after a while.And not see current wifi list.
My code:
Imports deneme2.NativeWifi
Imports System.Text
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Timer1.Start()
End Sub
Private Sub scan()
On Error Resume Next
Dim client As New WlanClient
ListView1.Items.Clear()
For Each wlanIface As WlanClient.WlanInterface In client.Interfaces
Dim networks() As Wlan.WlanAvailableNetwork = wlanIface.GetAvailableNetworkList(0)
For Each network As Wlan.WlanAvailableNetwork In networks
Dim ssid As Wlan.Dot11Ssid = network.dot11Ssid
Dim networkName As String = Encoding.ASCII.GetString(ssid.SSID, 0, CType(ssid.SSIDLength, Integer))
Dim item As ListViewItem = New ListViewItem(networkName)
item.SubItems.Add(network.dot11DefaultCipherAlgorithm.ToString())
item.SubItems.Add(network.wlanSignalQuality.ToString + "%")
ListView1.Items.Add(item)
Next
Next
End Sub
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
scan()
End Sub
End Class

vb.net Find and REMOVE a line in a textbox

I'm very frustrated trying to get my code to work.
I'm trying to have a selected item in a listbox removed also in the textbox.
Getting ready to remove text;
Removed the text;
But it's still in the textbox.
Here is my code
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ListBox1.Items.Add(TextBox1.Text)
TextBox2.Text += TextBox1.Text & vbNewLine
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
ListBox1.Items.Remove(ListBox1.SelectedItem)
'
'//HOW TO REMOVE THE SELECTED TEXT IN THE LISTBOX ALSO REMOVED IN THE TEXTBOX2??
'
'
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
Dim filenames As String = "C:\log\log.txt"
My.Computer.FileSystem.WriteAllText(filenames, TextBox2.Text, False)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim filenames As String = "C:\log\log.txt"
If My.Computer.FileSystem.FileExists(filenames) Then
TextBox2.Text = My.Computer.FileSystem.ReadAllText(filenames)
Dim items()
items = TextBox2.Lines()
For Each item In items
ListBox1.Items.Add(item)
Next
End If
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Clipboard.SetText(ListBox1.SelectedItem)
End Sub
End Class
The worst part is that every time I try to look it up online, there are no errors until I click the button that says 'Value Cannot Be Null'
It happened every single time.
Please, before you mash the -1 button, at least tell me why. I'm new to this.
This should work for you
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
TextBox1.Text = TextBox1.Text.Replace(ListBox1.Items(ListBox1.SelectedIndex), Nothing)
ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
End Sub
End Class

How do I store a text file inside a VB.net project?

I have built a basic VB app in the latest visual studio community. It's a simple app to load a text file into a list box then allow the list to be filtered down and finally the selected value to be copied.
It all works fine but I am curious, if I want to distribute this to other users I need to send them the text file (and the location is currently hard coded).
There has to be a better way to do this, do I need to import the text file as some sort of object in my project so it is then part of the project as opposed to a text file on its own?
Here is my code:
Public Class Form1
Dim MyArray() As String = My.Computer.FileSystem.ReadAllText("C:\Temp\Products.txt").Split(Environment.NewLine)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ListBox1.DataSource = MyArray
End Sub
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
TextBox2.Text = ListBox1.SelectedValue
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Clipboard.Clear()
Clipboard.SetText(TextBox2.Text)
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
Dim FilteredArray(0) As String
Dim ProdName As String
Dim X As Long = 0
ListBox1.DataSource = MyArray
For Each ProdName In ListBox1.Items
If InStr(UCase(ProdName), UCase(TextBox1.Text)) > 0 Then
ReDim Preserve FilteredArray(X)
FilteredArray(X) = ProdName
X = X + 1
End If
Next
ListBox1.DataSource = FilteredArray
End Sub
End Class
Any help is appreciated.
For completeness, here is my final solution:
Imports System.IO
Imports System.Reflection
Public Class Form1
Dim MyArray() As String
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Using sr As StreamReader = New StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream("WindowsApplication2.Products.txt"))
MyArray = Split(sr.ReadToEnd(), vbLf)
End Using
ListBox1.DataSource = MyArray
End Sub
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
TextBox2.Text = ListBox1.SelectedValue
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Clipboard.Clear()
Clipboard.SetText(TextBox2.Text)
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
Dim FilteredArray(0) As String
Dim ProdName As String
Dim X As Long = 0
ListBox1.DataSource = MyArray
For Each ProdName In ListBox1.Items
If InStr(UCase(ProdName), UCase(TextBox1.Text)) > 0 Then
ReDim Preserve FilteredArray(X)
FilteredArray(X) = ProdName
X = X + 1
End If
Next
ListBox1.DataSource = FilteredArray
End Sub
End Class
I think is a good way to store such file is resources of assembly.
Include your file into project. Set Build Action to Embedded Resource and then this file will be store inside the assembly.
How to read resource file from assembly you can find here: How to read embedded resource text file