How do I make my Array Search Tool More Robust

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hello,
I have been working on a tool that will compare two arrays (one with constants (Array 1), and the other created from an uploaded user file (Array2)). As it stands, the code will search for the words from Array 1 and list them in a new text file.
I am wondering how to make this program do two things:
1) How do I make it display the entire line that a constant is found on?
Example
Array 1 constant could be "Cloud"
Line in text will read "Cloud" = is huge and white (this could be any length and the word "Cloud" in Array 2 will be in Quotes)
Line in Array 2 will look similar to this "Cloud" = is huge and white
2) How do I make it so that all lines that contain the constant are displayed (not just the first instance)?

Here is the code that I am working with:

<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; Option Strict <span style="color:Blue; Off
<span style="color:Blue; Option <span style="color:Blue; Explicit <span style="color:Blue; On
<span style="color:Blue; Option Infer <span style="color:Blue; Off
<span style="color:Blue; Imports System
<span style="color:Blue; Imports System.IO
<span style="color:Blue; Imports System.<span style="color:Blue; String
<span style="color:Blue; Imports System.Windows.Forms
<span style="color:Blue; Imports Microsoft.VisualBasic.FileIO
<span style="color:Blue; Imports System.Text

<span style="color:Blue; Public <span style="color:Blue; Class Form1
<span style="color:Blue; Dim strCTPRegVals(108) <span style="color:Blue; As <span style="color:Blue; String
<span style="color:Blue; Dim InputFile <span style="color:Blue; As <span style="color:Blue; String
<span style="color:Blue; Dim BoxRegValues() <span style="color:Blue; As <span style="color:Blue; String

<span style="color:Blue; Private <span style="color:Blue; Sub Form1_Load(<span style="color:Blue; ByVal sender <span style="color:Blue; As System.Object, <span style="color:Blue; ByVal e <span style="color:Blue; As System.EventArgs) <span style="color:Blue; Handles <span style="color:Blue; Me.Load

strCTPRegVals(0) = <span style="color:#A31515; "LegalNoticeText"
strCTPRegVals(1) = <span style="color:#A31515; "LegalNoticeCaption"
strCTPRegVals(2) = <span style="color:#A31515; "DefaultPassword"
strCTPRegVals(3) = <span style="color:#A31515; "ProtectionMode"
strCTPRegVals(4) = <span style="color:#A31515; "EnablePortLocking"
strCTPRegVals(5) = <span style="color:#A31515; "EnhancedSecurityLevel"
strCTPRegVals(6) = <span style="color:#A31515; "LDAPServerIntegrity"
strCTPRegVals(7) = <span style="color:#A31515; "Os2LibPath"
strCTPRegVals(8) = <span style="color:#A31515; "Optional"
strCTPRegVals(9) = <span style="color:#A31515; "OS2"
strCTPRegVals(10) = <span style="color:#A31515; "POSIX"
strCTPRegVals(11) = <span style="color:#A31515; "GreetingMessage"
strCTPRegVals(12) = <span style="color:#A31515; "SCRNSAVE.EXE "
strCTPRegVals(13) = <span style="color:#A31515; "ScreenSaveTimeOut"
strCTPRegVals(14) = <span style="color:#A31515; "ScreenSaveActive"
strCTPRegVals(15) = <span style="color:#A31515; "ScreenSaverIsSecure"
strCTPRegVals(16) = <span style="color:#A31515; "DisableSavePassword"
strCTPRegVals(17) = <span style="color:#A31515; "NoRDS"
strCTPRegVals(18) = <span style="color:#A31515; "Security_HKLM_only "
strCTPRegVals(19) = <span style="color:#A31515; "Security_Options_Edit "
strCTPRegVals(20) = <span style="color:#A31515; "ProxySettingsPerUser"
strCTPRegVals(21) = <span style="color:#A31515; "NoJITSetup"
strCTPRegVals(22) = <span style="color:#A31515; "NoUpdateCheck"
strCTPRegVals(23) = <span style="color:#A31515; "PreventRun"
strCTPRegVals(24) = <span style="color:#A31515; "LogonType"
strCTPRegVals(25) = <span style="color:#A31515; "SyncForegroundPolicy"
strCTPRegVals(26) = <span style="color:#A31515; "NC_AllowNetBridge"
strCTPRegVals(27) = <span style="color:#A31515; "KMPrintersAreBlocked "
strCTPRegVals(28) = <span style="color:#A31515; "AuditBaseObjects"
strCTPRegVals(29) = <span style="color:#A31515; "FullPrivilegeAuditing"
strCTPRegVals(30) = <span style="color:#A31515; "CrashOnAuditFail"
strCTPRegVals(31) = <span style="color:#A31515; "AllocateDASD"
strCTPRegVals(32) = <span style="color:#A31515; "addprinterdrivers"
strCTPRegVals(33) = <span style="color:#A31515; "AllocateCDRoms"
strCTPRegVals(34) = <span style="color:#A31515; "AllocateFloppies"
strCTPRegVals(35) = <span style="color:#A31515; "DisablePasswordChange"
strCTPRegVals(36) = <span style="color:#A31515; "DontDisplayLastUsername"
strCTPRegVals(37) = <span style="color:#A31515; "AutoAdminLogon"
strCTPRegVals(38) = <span style="color:#A31515; "DisableCAD"
strCTPRegVals(39) = <span style="color:#A31515; "CachedLogonsCount"
strCTPRegVals(40) = <span style="color:#A31515; "PasswordExpiryWarning"
strCTPRegVals(41) = <span style="color:#A31515; "RequireSecuritySignature"
strCTPRegVals(42) = <span style="color:#A31515; "EnableSecuritySignature"
strCTPRegVals(43) = <span style="color:#A31515; "EnablePlainTextPassword"
strCTPRegVals(44) = <span style="color:#A31515; "AutoDisconnect"
strCTPRegVals(45) = <span style="color:#A31515; "RequireSecuritySignature"
strCTPRegVals(46) = <span style="color:#A31515; "EnableSecuritySignature"
strCTPRegVals(47) = <span style="color:#A31515; "EnableForcedLogoff"
strCTPRegVals(48) = <span style="color:#A31515; "RestrictAnonymous"
strCTPRegVals(49) = <span style="color:#A31515; "RestrictAnonymousSAM"
strCTPRegVals(50) = <span style="color:#A31515; "NullSessionPipes"
strCTPRegVals(51) = <span style="color:#A31515; "DisableDomainCreds"
strCTPRegVals(52) = <span style="color:#A31515; "EveryoneIncludesAnonymous"
strCTPRegVals(53) = <span style="color:#A31515; "NullSessionShares"
strCTPRegVals(54) = <span style="color:#A31515; "ForceGuest"
strCTPRegVals(55) = <span style="color:#A31515; "LMCompatibilityLevel"
strCTPRegVals(56) = <span style="color:#A31515; "SecurityLevel"
strCTPRegVals(57) = <span style="color:#A31515; "SetCommand"
strCTPRegVals(58) = <span style="color:#A31515; "ShutdownWithoutLogon"
strCTPRegVals(59) = <span style="color:#A31515; "ClearPageFileAtShutdown"
strCTPRegVals(60) = <span style="color:#A31515; "ProtectionMode"
strCTPRegVals(61) = <span style="color:#A31515; "Autorun"
strCTPRegVals(62) = <span style="color:#A31515; "RequireSignorSeal"
strCTPRegVals(63) = <span style="color:#A31515; "SealSecureChannel"
strCTPRegVals(64) = <span style="color:#A31515; "SignSecureChannel"
strCTPRegVals(65) = <span style="color:#A31515; "RequireStrongKey"
strCTPRegVals(66) = <span style="color:#A31515; "ScRemoveOption"
strCTPRegVals(67) = <span style="color:#A31515; "UndockWithoutLogon"
strCTPRegVals(68) = <span style="color:#A31515; "LimitBlankPasswordUse"
strCTPRegVals(69) = <span style="color:#A31515; "SubmitControl"
strCTPRegVals(70) = <span style="color:#A31515; "MaximumPasswordAge"
strCTPRegVals(71) = <span style="color:#A31515; "RestrictNullSessAccess"
strCTPRegVals(72) = <span style="color:#A31515; "NoLMHash"
strCTPRegVals(73) = <span style="color:#A31515; "ObCaseInsensitive"
strCTPRegVals(74) = <span style="color:#A31515; "FIPSAlgorithmPolicy"
strCTPRegVals(75) = <span style="color:#A31515; "EnableICMPRedirect"
strCTPRegVals(76) = <span style="color:#A31515; "SynAttackProtect"
strCTPRegVals(77) = <span style="color:#A31515; "EnableDeadGWDetect"
strCTPRegVals(78) = <span style="color:#A31515; "NoNameReleaseOnDemand"
strCTPRegVals(79) = <span style="color:#A31515; "ProtectionMode"
strCTPRegVals(80) = <span style="color:#A31515; "TcpMaxHalfOpen"
strCTPRegVals(81) = <span style="color:#A31515; "MaximumPasswordAge"
strCTPRegVals(82) = <span style="color:#A31515; "RefuseReset"
strCTPRegVals(83) = <span style="color:#A31515; "TcpMaxHalfOpenRetired"
strCTPRegVals(84) = <span style="color:#A31515; "ForceUnlockLogon"
strCTPRegVals(85) = <span style="color:#A31515; "DisableIPSourceRouting"
strCTPRegVals(86) = <span style="color:#A31515; "KeepAliveTime"
strCTPRegVals(87) = <span style="color:#A31515; "NoDefaultExempt"
strCTPRegVals(88) = <span style="color:#A31515; "NoDriveTypeAutoRun"
strCTPRegVals(89) = <span style="color:#A31515; "NtfsDisable8dot3NameCreation"
strCTPRegVals(90) = <span style="color:#A31515; "PerformRouterDiscovery"
strCTPRegVals(91) = <span style="color:#A31515; "SafeDllSearchMode"
strCTPRegVals(92) = <span style="color:#A31515; "WarningLevel"
strCTPRegVals(93) = <span style="color:#A31515; "SingleSessionPerUser"
strCTPRegVals(94) = <span style="color:#A31515; "KeepAliveEnable"
strCTPRegVals(95) = <span style="color:#A31515; "MaxInstanceCount"
strCTPRegVals(96) = <span style="color:#A31515; "fDenyTSConnections"
strCTPRegVals(97) = <span style="color:#A31515; "fWritableTSCCPermTab"
strCTPRegVals(98) = <span style="color:#A31515; "Shadow"
strCTPRegVals(99) = <span style="color:#A31515; "fPromptforPassword"
strCTPRegVals(100) = <span style="color:#A31515; "PerSessionTempDir"
strCTPRegVals(101) = <span style="color:#A31515; "DeleteTempDirsOnExit"
strCTPRegVals(102) = <span style="color:#A31515; "MaxDisconnectionTime"
strCTPRegVals(103) = <span style="color:#A31515; "MaxIdleTime"
strCTPRegVals(104) = <span style="color:#A31515; "fReconnectSame"
strCTPRegVals(105) = <span style="color:#A31515; "fResetBroken"
strCTPRegVals(106) = <span style="color:#A31515; "EnableAdminTSRemote"
strCTPRegVals(107) = <span style="color:#A31515; "fAllowToGetHelp "
strCTPRegVals(108) = <span style="color:#A31515; "fInheritInitialProgram"

<span style="color:Green; Dim rtb1 As New RichTextBox
<span style="color:Green; rtb1.Lines = strCTPRegVals
<span style="color:Green; My.Computer.FileSystem.WriteAllText(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "TestFile.txt", rtb1.Text, False)

<span style="color:Blue; End <span style="color:Blue; Sub

<span style="color:Blue; Private <span style="color:Blue; Sub btnArraysAsParam_Click(<span style="color:Blue; ByVal sender <span style="color:Blue; As System.Object, <span style="color:Blue; ByVal e <span style="color:Blue; As System.EventArgs) <span style="color:Blue; Handles btnArraysAsParam.Click
<span style="color:Green; List of registry keys needed to populate CTP document
AddItemsToList(strCTPRegVals)
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Private <span style="color:Blue; Sub AddItemsToList(<span style="color:Blue; ByVal arrayList() <span style="color:Blue; As <span style="color:Blue; String)
<span style="color:Green; Enumerates the PermRegVals list box with the keys being searched
<span style="color:Blue; For <span style="color:Blue; Each strReg <span style="color:Blue; As <span style="color:Blue; String <span style="color:Blue; In arrayList
<span style="color:Green; add the array item to the list
lstPermRegVals.Items.Add(strReg)
<span style="color:Green; txtPremRegVals.Text(strReg) = "Your CTP Keys Are Primed For Searching"
<span style="color:Blue; Next
<span style="color:Blue; End <span style="color:Blue; Sub

<span style="color:Blue; Private <span style="color:Blue; Sub btnBoxRegVals_Click(<span style="color:Blue; ByVal sender <span style="color:Blue; As System.Object, <span style="color:Blue; ByVal e <span style="color:Blue; As System.EventArgs) <span style="color:Blue; Handles btnBoxRegVals.Click

<span style="color:Blue; With OpenFileDialog1
.Filter = <span style="color:#A31515; "Text Documents (*.txt) | *.txt|All Files (*.*)| *.*"
.FilterIndex = 1
.Title = <span style="color:#A31515; "Load Registry File Into Array"
<span style="color:Blue; End <span style="color:Blue; With

<span style="color:Blue; If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK <span style="color:Blue; Then

<span style="color:Blue; Dim AllLines <span style="color:Blue; As <span style="color:Blue; New ArrayList
<span style="color:Green; InputFile = OpenFileDialog1.FileName

<span style="color:Green; If Not (OpenFileDialog1.FileName Is Nothing) Then
<span style="color:Blue; Using SR <span style="color:Blue; As <span style="color:Blue; New StreamReader(OpenFileDialog1.FileName)
<span style="color:Blue; Do <span style="color:Blue; While SR.Peek() >= 0
<span style="color:Blue; Dim currentLine <span style="color:Blue; As <span style="color:Blue; String = SR.ReadLine()
AllLines.Add(currentLine)
<span style="color:Green; lstBoxRegVals.Items.Add(currentLine)
<span style="color:Blue; Loop
<span style="color:Blue; End <span style="color:Blue; Using
BoxRegValues = AllLines.Cast(Of <span style="color:Blue; String).ToArray
<span style="color:Blue; End <span style="color:Blue; If

<span style="color:Blue; End <span style="color:Blue; Sub

<span style="color:Blue; Private <span style="color:Blue; Sub btnCompareArrays_Click(<span style="color:Blue; ByVal sender <span style="color:Blue; As System.Object, <span style="color:Blue; ByVal e <span style="color:Blue; As System.EventArgs) <span style="color:Blue; Handles btnCompareArrays.Click

<span style="color:Blue; Dim results <span style="color:Blue; As <span style="color:Blue; New List(Of <span style="color:Blue; String)
<span style="color:Blue; Dim ctpRegList <span style="color:Blue; As <span style="color:Blue; New List(Of <span style="color:Blue; String)
<span style="color:Blue; Dim boxRegList <span style="color:Blue; As <span style="color:Blue; New List(Of <span style="color:Blue; String)

<span style="color:Blue; For index <span style="color:Blue; As <span style="color:Blue; Integer = 0 <span style="color:Blue; To strCTPRegVals.GetUpperBound(0)
<span style="color:Blue; If <span style="color:Blue; Not strCTPRegVals(index) <span style="color:Blue; Is <span style="color:Blue; Nothing <span style="color:Blue; Then
ctpRegList.Add(strCTPRegVals(index))
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; Next

<span style="color:Blue; For index <span style="color:Blue; As <span style="color:Blue; Integer = 0 <span style="color:Blue; To BoxRegValues.GetUpperBound(0)
<span style="color:Blue; If <span style="color:Blue; Not BoxRegValues(index) <span style="color:Blue; Is <span style="color:Blue; Nothing <span style="color:Blue; Then
boxRegList.Add(BoxRegValues(index))
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; Next

<span style="color:Blue; Dim ctpRegArray() <span style="color:Blue; As <span style="color:Blue; String = ctpRegList.ToArray
<span style="color:Blue; Dim boxRegArray() <span style="color:Blue; As <span style="color:Blue; String = boxRegList.ToArray

<span style="color:Blue; For index <span style="color:Blue; As <span style="color:Blue; Integer = 0 <span style="color:Blue; To ctpRegArray.GetUpperBound(0)
<span style="color:Blue; If boxRegArray.Contains(ctpRegArray(index)) = <span style="color:Blue; True <span style="color:Blue; Then
results.Add(ctpRegArray(index))
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; Next

<span style="color:Blue; Dim sb <span style="color:Blue; As <span style="color:Blue; New System.Text.StringBuilder
<span style="color:Blue; Dim itm <span style="color:Blue; As <span style="color:Blue; String = sb.ToString

<span style="color:Blue; If results.<span style="color:Blue; Count() >= 0 <span style="color:Blue; Then
<span style="color:Blue; For index <span style="color:Blue; As <span style="color:Blue; Integer = 0 <span style="color:Blue; To results.<span style="color:Blue; Count - 1
sb.Append(results.Item(index).ToString & <span style="color:#A31515; ", " & vbCrLf)
<span style="color:Blue; Next
<span style="color:Blue; End <span style="color:Blue; If

<span style="color:Blue; Try
<span style="color:Blue; If sb.ToString <> <span style="color:Blue; String.Empty <span style="color:Blue; Then
<span style="color:Blue; My.Computer.FileSystem.WriteAllText(<span style="color:#A31515; "C:scriptstest.txt", sb.ToString, <span style="color:Blue; False)
<span style="color:Green; The following should open the text file using
<span style="color:Green; whatever the default text editor is (typically
<span style="color:Green; Notepad):
Process.Start(<span style="color:#A31515; "C:scriptstest.txt")
<span style="color:Green; ElseIf
<span style="color:Green; Throw New Exception("The file specified cannot be found.")
<span style="color:Blue; End <span style="color:Blue; If

<span style="color:Blue; Catch ex <span style="color:Blue; As Exception
MessageBox.Show(<span style="color:#A31515; "An error occurred:" & vbCrLf & vbCrLf & ex.Message, _
<span style="color:#A31515; "Program Error", MessageBoxButtons.OK, MessageBoxIcon.<span style="color:Blue; Error)
<span style="color:Blue; End <span style="color:Blue; Try

<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; End <span style="color:Blue; Class
[/code]
<br/>
Thanks for any help!





View the full article
 
Back
Top