Results 1 to 6 of 6

Thread: [RESOLVED] Need help with strings

  1. #1

    Thread Starter
    Hyperactive Member mbutler755's Avatar
    Join Date
    May 2008
    Location
    Peoria, AZ
    Posts
    417

    Resolved [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:
    1. ' Analyze button on original textbox
    2. ' Strips out the lines matching the items listed below in the keywords comment
    3. '
    4.     Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    5.         Dim OriginalFilePath As String = OriginalFileOpenFileDialog.FileName.ToString
    6.         Dim lines As New List(Of String)(IO.File.ReadAllLines(OriginalFilePath))
    7.         Dim searchtext As String = "Login"
    8.  
    9.         ' Keywords to strip out lines from the DEBOUT.TXT
    10.         Dim match = (From line In lines Where line.Contains("Login") _
    11.                      Or line.Contains("Username") Or line.Contains("Logout") _
    12.                      Or line.Contains("rpt") Or line.Contains("USERNAME") Select line).ToArray
    13.  
    14.         If match Is Nothing OrElse match.Length = 0 Then MsgBox("Nothing found") : Return
    15.  
    16.         For Each m As String In match
    17.             TextBox2.Text = "LINE " & lines.IndexOf(m).ToString & ": " & m & Environment.NewLine
    18.         Next
    19.     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.
    Last edited by mbutler755; Apr 25th, 2010 at 11:33 AM. Reason: removed archive of the program
    Regards,

    Matt Butler, MBA, BSIT/SE, MCBP
    Owner, Intense IT, LLC
    Find us on Facebook
    Follow us on Twitter
    Link up on LinkedIn
    mb (at) i2t.us

    CODE BANK SUBMISSIONS: Converting Images to Base64 and Back Again

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Need help with strings

    ok try this:
    Attached Files Attached Files

  3. #3

    Thread Starter
    Hyperactive Member mbutler755's Avatar
    Join Date
    May 2008
    Location
    Peoria, AZ
    Posts
    417

    Re: Need help with strings

    Quote Originally Posted by .paul. View Post
    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:
    Last edited by mbutler755; Apr 25th, 2010 at 11:29 AM. Reason: change wording
    Regards,

    Matt Butler, MBA, BSIT/SE, MCBP
    Owner, Intense IT, LLC
    Find us on Facebook
    Follow us on Twitter
    Link up on LinkedIn
    mb (at) i2t.us

    CODE BANK SUBMISSIONS: Converting Images to Base64 and Back Again

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Need help with strings

    try this:

    vb Code:
    1. Dim Savedfile As String
    2.  
    3. ' Button2 = Analyze.
    4. ' This sub opens a SaveFileDialog and starts the backgroundworker
    5. '
    6. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    7.     If SaveFileDialog1.ShowDialog = DialogResult.OK Then
    8.         Savedfile = SaveFileDialog1.FileName
    9.         BackgroundWorker1.RunWorkerAsync()
    10.     End If
    11. End Sub
    12.  
    13. ' Background worker writes the contents of textbox2 to a file (default file = AM-AUDIT.TXT)
    14. '
    15. Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    16.     My.Computer.FileSystem.WriteAllText(Savedfile, TextBox2.Text, True)
    17.     MsgBox("Process Complete.", MsgBoxStyle.OkOnly)
    18. End Sub

  5. #5

    Thread Starter
    Hyperactive Member mbutler755's Avatar
    Join Date
    May 2008
    Location
    Peoria, AZ
    Posts
    417

    Talking Re: Need help with strings

    Quote Originally Posted by mbutler755 View Post
    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!
    Regards,

    Matt Butler, MBA, BSIT/SE, MCBP
    Owner, Intense IT, LLC
    Find us on Facebook
    Follow us on Twitter
    Link up on LinkedIn
    mb (at) i2t.us

    CODE BANK SUBMISSIONS: Converting Images to Base64 and Back Again

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: [RESOLVED] Need help with strings

    the loading + analyzing are lengthy processes though...

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width