[RESOLVED] Need help with strings
I am building a program that strips out certain keywords from a much larger file. I am using code that .paul. provided in a different post, but I can't get it to work correctly. Here is the code that I am using:
vb.net Code:
' Analyze button on original textbox
' Strips out the lines matching the items listed below in the keywords comment
'
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim OriginalFilePath As String = OriginalFileOpenFileDialog.FileName.ToString
Dim lines As New List(Of String)(IO.File.ReadAllLines(OriginalFilePath))
Dim searchtext As String = "Login"
' Keywords to strip out lines from the DEBOUT.TXT
Dim match = (From line In lines Where line.Contains("Login") _
Or line.Contains("Username") Or line.Contains("Logout") _
Or line.Contains("rpt") Or line.Contains("USERNAME") Select line).ToArray
If match Is Nothing OrElse match.Length = 0 Then MsgBox("Nothing found") : Return
For Each m As String In match
TextBox2.Text = "LINE " & lines.IndexOf(m).ToString & ": " & m & Environment.NewLine
Next
End Sub
Basically, the first match will appear in textbox2, but no other matches appear after that. I put a breakpoint at the For Each loop and checked out the locals. I am showing 2,502 matches for the variable "match." I just now put another breakpoint in there and I can see that it is stepping, but nothing is recorded. Any help is much appreciated.
In the archive is a file named debout.txt. This file should be used for the testing.
1 Attachment(s)
Re: Need help with strings
Re: Need help with strings
Quote:
Originally Posted by
.paul.
ok try this:
Thanks Paul! This one works perfectly. The only thing that is broken now is writing the contents of textbox2 to a file. It seems like a cross-threading problem. The details are below:
Code:
System.Threading.ThreadStateException was unhandled by user code
Message=Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it. This exception is only raised if a debugger is attached to the process.
Source=System.Windows.Forms
StackTrace:
at System.Windows.Forms.FileDialog.RunDialog(IntPtr hWndOwner)
at System.Windows.Forms.CommonDialog.ShowDialog(IWin32Window owner)
at System.Windows.Forms.CommonDialog.ShowDialog()
at Debout_Auditor.Form1.BackgroundWorker1_DoWork(Object sender, DoWorkEventArgs e) in C:\Documents and Settings\mbutler\My Documents\Downloads\Debout Auditor\Debout Auditor\Form1.vb:line 53
at System.ComponentModel.BackgroundWorker.OnDoWork(DoWorkEventArgs e)
at System.ComponentModel.BackgroundWorker.WorkerThreadStart(Object argument)
InnerException:
Re: Need help with strings
try this:
vb Code:
Dim Savedfile As String
' Button2 = Analyze.
' This sub opens a SaveFileDialog and starts the backgroundworker
'
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If SaveFileDialog1.ShowDialog = DialogResult.OK Then
Savedfile = SaveFileDialog1.FileName
BackgroundWorker1.RunWorkerAsync()
End If
End Sub
' Background worker writes the contents of textbox2 to a file (default file = AM-AUDIT.TXT)
'
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
My.Computer.FileSystem.WriteAllText(Savedfile, TextBox2.Text, True)
MsgBox("Process Complete.", MsgBoxStyle.OkOnly)
End Sub
Re: Need help with strings
Quote:
Originally Posted by
mbutler755
Thanks Paul! This one works perfectly. The only thing that is broken now is writing the contents of textbox2 to a file. It seems like a cross-threading problem. The details are below:
Code:
System.Threading.ThreadStateException was unhandled by user code
Message=Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it. This exception is only raised if a debugger is attached to the process.
Source=System.Windows.Forms
StackTrace:
at System.Windows.Forms.FileDialog.RunDialog(IntPtr hWndOwner)
at System.Windows.Forms.CommonDialog.ShowDialog(IWin32Window owner)
at System.Windows.Forms.CommonDialog.ShowDialog()
at Debout_Auditor.Form1.BackgroundWorker1_DoWork(Object sender, DoWorkEventArgs e) in C:\Documents and Settings\mbutler\My Documents\Downloads\Debout Auditor\Debout Auditor\Form1.vb:line 53
at System.ComponentModel.BackgroundWorker.OnDoWork(DoWorkEventArgs e)
at System.ComponentModel.BackgroundWorker.WorkerThreadStart(Object argument)
InnerException:
This was a super easy fix. I just removed the background worker from starting. The file is written so quickly, that it wasn't required. Thanks for your help Paul!
Re: [RESOLVED] Need help with strings
the loading + analyzing are lengthy processes though...