How to read text from a file and populating a message?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kieth
    New Member
    • May 2010
    • 8

    How to read text from a file and populating a message?

    Does anybody know how to read from a text file and if it contains a certain word or phrase (for example - error) to then flag up the error?
    if possible...quot ing the whole line of text the "Error" is on?
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Well first you need to read the file into memory.
    Then I would probably use Regular Expressions to find any line that contains the word that you're searching for. If that word exists then just use the MessageBox (assuming this is a desktop application and not a web application) to display your message.

    What part are you stuck on?

    Comment

    • kieth
      New Member
      • May 2010
      • 8

      #3
      thank you for the reply,

      this is what i have, but at the minute... i dont know what it's doing because the cmd box opens and closes instantly not allowing me to see any results
      Code:
        {
                  FileStream fout;
      
                  System.IO.FileInfo f = new System.IO.FileInfo("C:\\Documents and Settings\\kwareham01\\Desktop\\test log.txt");
                  // System.IO.FileStream s = f.Open(System.IO.FileMode.Open, System.IO.FileAccess.Read);
                  //  Console.WriteLine("Entry does not exist in database");
                  StreamReader s = f.OpenText();
                  string read = null;
                  while ((read = s.ReadLine()) != null)
      
                      if (read.Contains("Entry does not exist in database"))
                      {
                 
                          // open output file 
                          try
                          {
                              fout = new FileStream("test.txt", FileMode.Create);
                          }
                          catch (IOException exc)
                          {
                              Console.WriteLine(exc.Message + "\nError Opening Output File");
                              return;
                          }
      Last edited by Frinavale; May 27 '10, 02:54 PM. Reason: Please post code in [code] ... [/code] tags. Added code tags.

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Well maybe that's because it looks like you're writing a Console application?
        What kind of application are you developing?

        Comment

        • kieth
          New Member
          • May 2010
          • 8

          #5
          sorry if it's unclear, i'm still very new to programming...

          I am trying to develop a console app, that will run through error logs, if it finds an error it flags it up...

          to save the manual process of reading through lots of logs.

          i may well just be misguided in my approach, in which case it will be back to the books...

          just thought i'd pop it on here on the off chance

          thanks

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            Hehe sorry that's my fault...for some weird reason I thought you were trying to mix the two types of applications together (it doesn't work well).

            In order to see what's happen add a Console.ReadLin e() to the end of your main method....this will cause the console application to wait for the user to hit "enter" before closing :)

            -Frinny

            Comment

            • kieth
              New Member
              • May 2010
              • 8

              #7
              sorted, thank you

              Comment

              Working...