Listbox duplicates
Listbox duplicates
Listbox duplicates
Listbox duplicates
Listbox duplicates
Listbox duplicates Listbox duplicates Listbox duplicates Listbox duplicates Listbox duplicates Listbox duplicates Listbox duplicates Listbox duplicates
Listbox duplicates Listbox duplicates
Listbox duplicates
Go Back  Xtreme Visual Basic Talk > > > Listbox duplicates


Reply
 
Thread Tools Display Modes
  #1  
Old 05-31-2004, 07:43 PM
jast1980 jast1980 is offline
Newcomer
 
Join Date: Feb 2004
Posts: 18
Default Listbox duplicates


I have been messing with this for a while. I am trying to have the machine list hotfixes installed on the computer to a listbox and in turn then output to a file. I have a single call for the Function however it is like it is executing two times. I verified this by having a message box popup with the count of files. The count comes back correct but it pops up twice, as if running twice. Thanks for any help.

Private Function ReadRegistrySettings()

'Dim hotfix As Microsoft.Win32.RegistryKey
Dim subKeyName As String
Dim reg As String = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Hotfix"
Dim hotfix = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(reg, True)
Dim cnt As Integer
cnt = 0

For Each subKeyName In hotfix.GetSubkeyNames

' Microsoft.Win32.Registry.LocalMachine.Close()
'Dim tempKey As Microsoft.Win32.RegistryKey = _
'hotfix.OpenSubKey(subKeyName)

ListBox1.Items.Add(subKeyName)

cnt = cnt + 1
Dim ListFile As New System.IO.StreamWriter("Alreadyinstalled.txt")

Dim i As String
For Each i In ListBox1.Items
ListFile.WriteLine(i)
Next
ListFile.Close()

Next

MsgBox(cnt)

End Function

Private Sub Status_PanelextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Status.TextChanged
'Dim objRegistryKey As Microsoft.Win32.RegistryKey
'Dim strLocation As String = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\CSDVersion"
Dim rk, rk2 As Microsoft.Win32.RegistryKey
Dim vers, prod As String

rk = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\W indows NT\CurrentVersion", True)
rk2 = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\W indows NT\CurrentVersion", True)
prod = rk2.GetValue("ProductName")
vers = rk.GetValue("CSDVERSION")







Call ReadRegistrySettings()
'rk.GetValue("CSDVERSION")
Status.Text = prod & " " & vers
End Sub




End Class
Reply With Quote
  #2  
Old 05-31-2004, 08:36 PM
sonicdemonic sonicdemonic is offline
Junior Contributor
 
Join Date: Feb 2004
Location: Indiana
Posts: 261
Default

It is not running twice, or so to say..

See where you used
For Each subKeyName In hotfix.GetSubkeyNames

Then you have
cnt = cnt + 1
In the middle


Then you have
Next



There for.. it is running again because it found 2 entries

Move you counter variable below the next statement
Reply With Quote
  #3  
Old 05-31-2004, 08:48 PM
jast1980 jast1980 is offline
Newcomer
 
Join Date: Feb 2004
Posts: 18
Default

I should point out that when my function if called when pressing a button works just fine. However I want it to be called at the start of the application. Also as you can see the file prints out a text file stating the updates installed. I want to compare all the lines and if there is a name in one list that is not in the other I want it to remove that from the list. EX
1.txt has:
KB833567
Q345678
K234557

2.txt has:
KB833567
Q345678
K234557
K453452

I want it to create a new document and keep only the K453452 entry or any othersthat are present in 2.txt but not present in 1.txt
Reply With Quote
  #4  
Old 05-31-2004, 09:03 PM
sonicdemonic sonicdemonic is offline
Junior Contributor
 
Join Date: Feb 2004
Location: Indiana
Posts: 261
Default

I understand what you are saying so far, but..

Where is the second txt doc coming from?
Or are you replacing the original file with a new one?

Was I correct about the counter being in the wrong place?

So you are going to end up with 3 txt docs?
Reply With Quote
  #5  
Old 05-31-2004, 09:15 PM
jast1980 jast1980 is offline
Newcomer
 
Join Date: Feb 2004
Posts: 18
Default

Quote:
Originally Posted by sonicdemonic
I understand what you are saying so far, but..

Where is the second txt doc coming from?
Or are you replacing the original file with a new one?

Well before I go to far, basically what I am trying to do is create a program that will install any hotfixes that or not installed for Windows XP. The second txt file is going to be generated and updated by myself manually. Any idea why the function works when a button is pressed but not when called?
Reply With Quote
  #6  
Old 05-31-2004, 09:19 PM
sonicdemonic sonicdemonic is offline
Junior Contributor
 
Join Date: Feb 2004
Location: Indiana
Posts: 261
Default

Ok, you are going to have a text file that has a list of all the hot fixes that can be installed.
Then you want to scan for current hotfixs and save it to a text file.
compare the difference between the two and install what has not been installed.. correct..


Call ReadRegistrySettings()
You dont have to use "CALL" in the statement above..
Just use
Code:
ReadRegistrySettings()
Reply With Quote
  #7  
Old 05-31-2004, 09:22 PM
sonicdemonic sonicdemonic is offline
Junior Contributor
 
Join Date: Feb 2004
Location: Indiana
Posts: 261
Default

Something else you could do is return the fix list in you function variable .. then you wont have to create a txt file until after you have determined that you need one.
Reply With Quote
  #8  
Old 05-31-2004, 09:30 PM
sonicdemonic sonicdemonic is offline
Junior Contributor
 
Join Date: Feb 2004
Location: Indiana
Posts: 261
Default

You are on the right track.. You didnt show a whole lot of code and though I have not ran your code, because I dont have it all, i can *not give you complete answers. If you are getting the values from the registry that you want, you should start creating case statements to compare each registry entry against you list of available hotfixes.
Reply With Quote
  #9  
Old 05-31-2004, 10:24 PM
sonicdemonic sonicdemonic is offline
Junior Contributor
 
Join Date: Feb 2004
Location: Indiana
Posts: 261
Default

This is for the thread you start a while back about .net framework and autorun from CD. You could create a small vb6 program to check for .net framework and install if needed, otherwaise it would run your .net program.
Reply With Quote
  #10  
Old 05-31-2004, 10:32 PM
sonicdemonic sonicdemonic is offline
Junior Contributor
 
Join Date: Feb 2004
Location: Indiana
Posts: 261
Default

Ok, Since you have not replied I will just continue from before,
you said that you was creating a text doc to contain all of your hotfixes,
Though this is a minor detail I would make it a .dat file just so that people that dont know what they are doing dont access it with the notepad easily.
Next,
You might want to try using my registry module , it might make you code easier to read.
Registry module
Also
I have changed your code from before..
Code:
Private Function ReadRegistrySettings() as String 'Dim hotfix As Microsoft.Win32.RegistryKey Dim subKeyName As String Dim reg As String = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Hotfix" Dim hotfix = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(reg, True) Dim cnt As Integer = 0 For Each subKeyName In hotfix.GetSubkeyNames ' Microsoft.Win32.Registry.LocalMachine.Close() 'Dim tempKey As Microsoft.Win32.RegistryKey = _ 'hotfix.OpenSubKey(subKeyName) ListBox1.Items.Add(subKeyName) Next cnt = cnt + 1 Dim i As String For Each i In ListBox1.Items ReadRegistrySettings = ReadRegistrySettings + i Next MsgBox(cnt) msgbox(ReadRegistrySettings) End Function

Before I made changes your next statements overlapped.. that's why it was running twice.. everytime the first one would run the second one was comparing everything in you Listbox.. and looping

Last edited by sonicdemonic; 05-31-2004 at 10:39 PM.
Reply With Quote
  #11  
Old 05-31-2004, 10:36 PM
sonicdemonic sonicdemonic is offline
Junior Contributor
 
Join Date: Feb 2004
Location: Indiana
Posts: 261
Default

What happens here is that instead for adding each line to a text file it added it to the variable (ReadRegistrySettings). So now you can compare the variables in it to you variables in your text documents. also you could compare all those line when it get the information from the registry. That would be the best way.
Reply With Quote
  #12  
Old 05-31-2004, 10:52 PM
sonicdemonic sonicdemonic is offline
Junior Contributor
 
Join Date: Feb 2004
Location: Indiana
Posts: 261
Default

Here is more updated code..

Code:
Private Function ReadRegistrySettings() as String 'Dim hotfix As Microsoft.Win32.RegistryKey Dim subKeyName As String Dim reg As String = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Hotfix" Dim hotfix = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(reg, True) Dim cnt As Integer = 0 For Each subKeyName In hotfix.GetSubkeyNames ' Microsoft.Win32.Registry.LocalMachine.Close() 'Dim tempKey As Microsoft.Win32.RegistryKey = _ 'hotfix.OpenSubKey(subKeyName) ListBox1.Items.Add(subKeyName) 'Compare to text file Dim varFile As String, LineofText As String, AllText As String varFile = srcdir & "data\CurHotFix.dat" Try FileOpen(1, varFile, OpenMode.Input) Do Until EOF(1) Select Case LineInput(1) Case subKeyName 'Match found here, put code for hadling data End Select Loop FileClose(1) Catch ex As Exception TxtMain.Text = "Error!" & vbNewLine & "Text file did not open" End Try Next cnt = cnt + 1 Dim i As String For Each i In ListBox1.Items ReadRegistrySettings = ReadRegistrySettings + i Next MsgBox(cnt) msgbox(ReadRegistrySettings) End Function

Here as it finds the hotfixes listed in the registry it check against you list of known fixes. From there just put you code hadling in the space shown above.
Reply With Quote
  #13  
Old 06-01-2004, 07:06 PM
jast1980 jast1980 is offline
Newcomer
 
Join Date: Feb 2004
Posts: 18
Default Thanks

Thanks for your help, I had actually tried something new right after I sent my last message yesterday and it seemed to work well. I had the program write the file and then import it in to the Listbox after it was done. My only concern is that say I have installed updates, but not in the order I am reading them, I want the program to scan each line of Possible Updates against those that are installed and deleting any that match, thereby leaving a list of the files that need to be updated. I hope that made sense to someone other than me.

Basically :
I have 1 , 2 ,3 , and 5 installed
There are updates 1, 2, 3, 4, 5, & 6
I want the program to compare all of them and then only keep the ones that it cannot match... ex 4 and 6
If I do it line to line then it will detect 5 as not installed because it will not match. I am working on the code, but I think it is time to take a break, the room is spinning
Reply With Quote
  #14  
Old 06-01-2004, 10:44 PM
jast1980 jast1980 is offline
Newcomer
 
Join Date: Feb 2004
Posts: 18
Default

Here is most of my code, just the stuff we are working on, the ReadRegistrySettings function writes the Alreadinstalled.dat file, Label1 then reads from it. I have been able to see that it is counting the amount of files in Hotfix.dat (the file that has the full list of updates). I was able to get it to write to the text file updates.dat, I tried working with the code you had but now I am totally lost... I do appreciate your patience and help on this. This is the part that is hard, once this is done I know how to code it to install the updates just fine...
I apologize again for my noobnes


Code:
Private Function ReadRegistrySettings() 'Dim hotfix As Microsoft.Win32.RegistryKey Dim subKeyName As String Dim reg As String = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Hotfix" Dim hotfix = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(reg) Dim cnt As String cnt = 0 For Each subKeyName In hotfix.GetSubkeyNames cnt = cnt + 1 Dim ListFile As New System.IO.StreamWriter("Alreadyinstalled.dat") Dim i As String For Each i In hotfix.Getsubkeynames ListFile.WriteLine(i) Next ListFile.Close() Next End Function Private Sub Status_PanelextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Status.TextChanged 'Dim objRegistryKey As Microsoft.Win32.RegistryKey 'Dim strLocation As String = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\CSDVersion" Dim rk, rk2 As Microsoft.Win32.RegistryKey Dim vers, prod As String rk = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows NT\CurrentVersion", True) rk2 = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows NT\CurrentVersion", True) prod = rk2.GetValue("ProductName") vers = rk.GetValue("CSDVERSION") ReadRegistrySettings() Status.Text = prod & " " & vers End Sub Private Sub ImportList(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Try Dim file_name As String = "AlreadyInstalled.dat" Dim stream_reader As New IO.StreamReader(file_name) Dim line As String Dim i As String i = 0 ' Read the file one line at a time. line = stream_reader.ReadLine() Do While Not (line Is Nothing) 'trim and make sure the line isn't blank. line = line.Trim() If line.Length > 0 Then _ ListBox1.Items.Add(line) i = i + 1 ' Get the next line. line = stream_reader.ReadLine() list.Text = "You have " & i & " Updates Installed" Loop ListBox1.SelectedIndex = 0 stream_reader.Close() Catch exc As Exception ' Report all errors. MsgBox(exc.Message, MsgBoxStyle.Exclamation, "Read " & _ "Error") End Try 'Compare to text file Dim LineofText As String, AllText As String Dim NotInstalledCounter As Integer = 0 Dim FindNotinstalled() As Boolean Dim Counter As Integer = 0 Dim HotfixArray() As String Dim inst As String = "AlreadyInstalled.dat" Dim stream_inst As New IO.StreamReader(inst) Dim HotFix As String = "CurHotFix.dat" 'Dim stream_avail As New IO.StreamReader(HotFix) Dim updates As String = "Updates.dat" Dim stream_write As New IO.StreamWriter(updates) Dim fileinstall As String Dim avail As String Dim cnt As String cnt = 0 fileinstall = stream_inst.ReadLine() ' avail = stream_avail.ReadLine() Try FileOpen(1, HotFix, OpenMode.Input) Do Until EOF(1) For Each fileinstall In stream_inst.ReadLine Select Case fileinstall Case Is = LineInput(1) FindNotinstalled(Counter) = True Case Is <> LineInput(1) ListBox2.Items.Add(LineInput(1)) End Select Next 'Select Case FindNotinstalled(Counter) ' Case False ' NotInstalledCounter = NotInstalledCounter + 1 ' Count not found Entries ' HotfixArray(Counter) = fileinstall ' Saves the subname in array ' ListBox2.Items.Add(HotfixArray(Counter)) ' Adds the entry to the listbox ' Counter = Counter + 1 ' End Select Loop Catch ex As Exception MsgBox("Error!" & vbNewLine & "Text file did not open") End Try FileClose(1) End Sub
Reply With Quote
  #15  
Old 06-01-2004, 11:20 PM
jast1980 jast1980 is offline
Newcomer
 
Join Date: Feb 2004
Posts: 18
Default

Quote:
Originally Posted by sonicdemonic
You should not have to save the installed list since you don't want to install a fix if you already installed it the text/dat file did not update..
So I would have it check each time.. That way you know for sure that you list is up-to-date..
The install list was my way of getting a list of the updates that have not yet been installed. That way I can have them read and then installed when the user hits a button. I do not want it to automatically install them, only when a button is pushed.
Reply With Quote
  #16  
Old 06-01-2004, 11:49 PM
jast1980 jast1980 is offline
Newcomer
 
Join Date: Feb 2004
Posts: 18
Default

The only issue I am encountering with the code you have is at HotfixArray(1, Counter) it says the number of indices exceeds the number of dimensions of the indexed array. And msg for the arrays was not working.
Reply With Quote
  #17  
Old 06-01-2004, 11:58 PM
jast1980 jast1980 is offline
Newcomer
 
Join Date: Feb 2004
Posts: 18
Default

Here it is, just rename to the .dat... again I really appreciate the help
Attached Files
File Type: txt CurHotFix.txt (504 Bytes, 1 views)
Reply With Quote
  #18  
Old 06-02-2004, 12:11 AM
jast1980 jast1980 is offline
Newcomer
 
Join Date: Feb 2004
Posts: 18
Default

Ya, I gotta hit the hay tonight, basically with the code... I am getting no errors, however there is nothing being displayed in the Listbox. When I attempt to launch the function it does not do anything. If I create a button to call it it gives an error, cannot open file.
Reply With Quote
  #19  
Old 06-02-2004, 07:16 PM
sonicdemonic sonicdemonic is offline
Junior Contributor
 
Join Date: Feb 2004
Location: Indiana
Posts: 261
Cool

As it turns out there were multiple problems throught-out the code but was just to tired last night to find'em plus having that text file to check against made it alot easier. Working updated code is below!

Put all this code into your form.. dont use a module for now.
You may not need all these imports but you might want to add them to test it out first.
Code:
Imports System.IO Imports System.Threading.ThreadState Imports System Imports Microsoft.Win32

Use this to call the function from where every you want in the current form.
Code:
CheckHotFixes()

Make sure that you use this code in the form.. For some reason I couldn't get it to post the string to the listbox from the module.
Code:
Function CheckHotFixes() Dim subKeyName As String Dim reg As String = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Hotfix" Dim hotfix = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(reg) Dim HotfixArray(2, 100) As String Dim FoundMatch As Boolean = False Dim Counter As Integer = 0 Dim InstalledCounter As Integer = 0 Dim NotInstalledCounter As Integer = 0 Dim LineofText As String FileOpen(1, "C:\CurHotFix.dat", OpenMode.Input) Do Until EOF(1) LineofText = LineInput(1) FoundMatch = False For Each subKeyName In hotfix.GetSubkeyNames Select Case LineofText Case Is = subKeyName FoundMatch = True End Select Next Select Case FoundMatch Case False HotfixArray(0, NotInstalledCounter) = LineofText ' Saves the subname in array ListBox1.Items.Add(HotfixArray(0, NotInstalledCounter)) ' List not install items NotInstalledCounter = NotInstalledCounter + 1 ' Count not found Entries ' if you want to store NON-installed entries in a txt file put that code here. Case True HotfixArray(1, InstalledCounter) = subKeyName ' Saves the subname in array 'ListBox1.Items.Add(HotfixArray(0, fixnotfound)) ' Use this only if you want to list installed items InstalledCounter = InstalledCounter + 1 ' Count not found Entries ' if you want to store installed entries in a txt file put that code here. End Select Counter = Counter + 1 Loop FileClose(1) End Function

I just tried this and it was working for me on my system. let me know if it works for you.

Last edited by sonicdemonic; 06-02-2004 at 07:52 PM.
Reply With Quote
  #20  
Old 06-02-2004, 08:12 PM
jast1980 jast1980 is offline
Newcomer
 
Join Date: Feb 2004
Posts: 18
Talking

All I can say is that you are a wonderful person!!!!!
I added a few extra lines to Cur Hot Fixes and it outputs them to Listbox!!!!

I think that was the toughest part, I *should* be able to do the rest myself but we will see how it plays out. I will be sure to add a little snippet in it giving you credit for the hard stuff

This helped me alot.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Listbox duplicates
Listbox duplicates
Listbox duplicates Listbox duplicates
Listbox duplicates
Listbox duplicates
Listbox duplicates Listbox duplicates Listbox duplicates Listbox duplicates Listbox duplicates Listbox duplicates Listbox duplicates
Listbox duplicates
Listbox duplicates
 
Listbox duplicates
Listbox duplicates
 
-->