Page 1 of 2 12 LastLast
Results 1 to 40 of 43

Thread: Getting specified text from text file

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2011
    Posts
    115

    Getting specified text from text file

    I whould like to get some names from a text file inside of it.

    For example my text file looks like this :

    Code:
    SERVER: New client joined this server...
    >>> Player "NorbyTKD" is connecting...
    >>> Player "NorbyTKD" joined the match.
    >>> Player "NorbyTKD" has entered the game.
    SERVER: Client closed by server...
    >>> Player "NorbyTKD" disconnected.
    SERVER: Client closed by server...
    >>> Player "Morten Degn" disconnected.
    SERVER: Client closed by server...
    >>> Player "John" disconnected.
    I need the line >>> Player "NorbyTKD" joined the match. only the player name
    in this case : NorbyTKD listed in textbox and when this user is disconnected like in this line :>>> Player "NorbyTKD" disconnected. it must read this also and delete the disconnected user from textbox.

    text box can have over 1000 lines depence on server runtime.

    Thanks

  2. #2
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Getting specified text from text file

    VB Code:
    1. using (StreamReader sr = new StreamReader("file.txt"))
    2.             {
    3.                 while (sr.Peek() > -1)
    4.                 {
    5.                     string ln = sr.ReadLine();
    6.                     if (ln.Contains(@""""))
    7.                     {
    8.                         string values = (from Match m in Regex.Matches(ln, @"""(.*?)""", RegexOptions.IgnoreCase)
    9.                                          select m.Value).ToArray()[0];
    10.  
    11.                         textBox1.Text += values + Environment.NewLine;
    12.                     }
    13.                 }
    14.             }

    This will return to you all the names from that file.
    Last edited by AceInfinity; Apr 12th, 2012 at 12:04 PM.
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Dec 2011
    Posts
    115

    Re: Getting specified text from text file

    Quote Originally Posted by AceInfinity View Post
    VB Code:
    1. String = @">>> Player ""Morten Degn"" disconnected.""";
    2. string[] values = (from Match m in Regex.Matches(InputString, @"""(.*?)""", RegexOptions.IgnoreCase)
    3.                    select m.Value).ToArray();
    4.  
    5. textBox1.Text = String.Join(",", values);

    This will return to you "Morten Degn", just as an example
    ,,

    if I put your code to a button I have 5 errors. also it must read first the test.txt and get the names from there. the disconnected and connected names are all random names . that we can not know.

  4. #4
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Getting specified text from text file

    I edited my post, now try. You should attempt this yourself you know. Reading from a file there's several methods you can use, i've shown you one now. That first code I posted was more of an example, and was not meant to be copied out directly into your IDE for it to automatically work.

    Cheers
    ~Ace
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Dec 2011
    Posts
    115

    Re: Getting specified text from text file

    Quote Originally Posted by AceInfinity View Post
    I edited my post, now try. You should attempt this yourself you know. Reading from a file there's several methods you can use, i've shown you one now.

    Cheers
    errors

    Code:
    Error	1	The name 'Regex' does not exist in the current context	
    Error	2	The name 'RegexOptions' does not exist in the current context	
    Error	3	The type or namespace name 'Match' could not be found (are you missing a using directive or an assembly reference?)

  6. #6
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Getting specified text from text file

    Quote Originally Posted by deve View Post
    errors

    Code:
    Error	1	The name 'Regex' does not exist in the current context	
    Error	2	The name 'RegexOptions' does not exist in the current context	
    Error	3	The type or namespace name 'Match' could not be found (are you missing a using directive or an assembly reference?)
    You're looking just to copy out the code that I provide, it doesn't exist becauase you havent declared that you're using Regex from the namespace it resides in.

    http://msdn.microsoft.com/en-us/libr...ons.regex.aspx

    It won't help you if you don't attempt to take the initiative to problem solve and learn individually.
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Dec 2011
    Posts
    115

    Re: Getting specified text from text file

    sorry I did now , I was just following you

    using System.Text.RegularExpressions;

    but there is a smal lproblem it list everything that has " " like there is things like
    LOG file "Game.LOG" created in 04/12/12 14:15:38 it does show now the Game.LOG also , just the connected players I wont and also needs to check if user is disconnected not list it.

  8. #8
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Getting specified text from text file

    using System.Text.RegularExpressions;
    Yes!! Good job

    "Game.LOG" did not exist in the sample you gave me however, so I minimized it to check for quotes only

    To remove them you can use the Replace Function, or maybe even the SubString Method if you wanted.. I'll see how this can be implemented and provide an example.

    EDIT: I see however 3 things...
    1. is connecting...
    2. joined the match.
    3. has entered the game.

    Do you want me to display just [Connected] instead of these entries or keep this wording? And it would be very helpful if you could provide me with more format to deal with, as i've mentioned: Game.LOG doesn't exist in the current snippet of log file that you have posted to me so far.
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Dec 2011
    Posts
    115

    Re: Getting specified text from text file

    thanks but I did write on my first post that it needs to delete it from the list also if user disconnected.

  10. #10
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Getting specified text from text file

    Alright but i'm still confused, as mentioned there are more than one indicator of a [Connection]

    1. is connecting...
    2. joined the match.
    3. has entered the game.

    Do you want me to keep this wording, or find these lines and replace them all with [Connected] or [Connecting] or something?

    Example:
    - John A [Connecting]
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Dec 2011
    Posts
    115

    Re: Getting specified text from text file

    this line is important : 2. joined the match. We need the user on this line without " " in to textbox and when this user have a disconnect it must delete from the list.

  12. #12
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Getting specified text from text file

    Why not use a ListView or ListBox in this case? And if they are disconnected, and that user gets removed, then shouldn't all of the existing entries of users be listed as "connected" anyways? Therefore I don't see a reason to provide that bit of information in the control.
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Dec 2011
    Posts
    115

    Re: Getting specified text from text file

    I can use Listview no problem anything that does the trick. so far I have changed your code like this :

    if (ln.Contains(@""" joined the match."))

    it just shows connected people alltime and not those people who are connected online. Thats why I need it to check also if it is disconnected deleted from the lsit so I have only online users.

  14. #14
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Getting specified text from text file

    Also I have more questions as this is getting really blurry for me. If this is for a game (seemingly), and you want to remove these names when they say they are disconnected, would this not mean that you would have to read this file on an interval to read the last line to determine whether to add or remove an entry from the data? I can't see this file being updated directly, and instead, being appended to, meaning the last line is the only important piece of information here. And to have to read it at an interval, you may miss some things if you don't read it fast enough, as if 10 people join in a second. How are you supposed to know where you last left off reading the file and closing it to determine where to being reading to update the data?

    Your edit makes sense, in the beginning I had no idea what you wanted though exactly, so this is what I began doing:

    vb Code:
    1. using (StreamReader sr = new StreamReader("file.txt"))
    2. {
    3.     while (sr.Peek() > -1)
    4.     {
    5.         string ln = sr.ReadLine();
    6.         if (ln.Contains("Player"))
    7.         {
    8.             string values = (from Match m in Regex.Matches(ln, @"""(.*?)""", RegexOptions.IgnoreCase)
    9.                              select m.Value).ToArray()[0];
    10.  
    11.             string status = ln.Contains("disconnected") ? " [Disconnected]" : " [Connected]";
    12.             listBox1.Items.Add(values.Replace((char)34, '\n') + status + Environment.NewLine);
    13.         }
    14.     }
    15. }
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Dec 2011
    Posts
    115

    Re: Getting specified text from text file

    it has System.IndexOutOfRangeException when debug

  16. #16
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Getting specified text from text file

    Ok, from the sample log you gave me though it works, so it must be because of some entry in the log you're analyzing. I can't help you with that until I get a larger sample log...

    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  17. #17

    Thread Starter
    Lively Member
    Join Date
    Dec 2011
    Posts
    115

    Re: Getting specified text from text file

    Quote Originally Posted by AceInfinity View Post
    Ok, from the sample log you gave me though it works, so it must be because of some entry in the log you're analyzing. I can't help you with that until I get a larger sample log...

    Code:
    -------------------------------------------------------------------------
      G A M E   R E S U L T S
    -------------------------------------------------------------------------
       Location  : Utah
       Season    : Pre-Rut
       Time      : 04/12/12 14:32:49
       Duration  : 15 minutes
       Game type : Best_Trophy
       Difficulty: Expert
       Result    : Game_has_a_winner!
       Players   : 2
          1. "Charles Bronson" is a Winner!
             Trophy(ies) : 14
             Score       : 393.331
             Best Trophy : Male Whitetail 
                Points   : Total (393.331) = Animal score (168.023)
                Distance : 86.29 meters
          2. "YONAH"
    -------------------------------------------------------------------------
    SERVER: Client closed by server...
    >>> Player "YONAH" disconnected.
    SERVER: Client closed by server...
    >>> Player "Charles Bronson" disconnected.
    >>> Game started.
    -------------------------------------------------------------------------
      G A M E   R E S U L T S
    -------------------------------------------------------------------------
       Location  : Utah
       Season    : Pre-Rut
       Time      : 04/12/12 14:48:24
       Duration  : 15 minutes
       Game type : Best_Trophy
       Difficulty: Expert
       Result    : Time_limit_hit.
       Players   : 0
    -------------------------------------------------------------------------
    SERVER: New client joined this server...
    >>> Player "Daniel" is connecting...
    >>> Player "Daniel" joined the match.
    >>> Game started.
    >>> Player "Daniel" has entered the game.
    SERVER: Client closed by server...
    >>> Player "Daniel" disconnected.
    SERVER: New client joined this server...
    >>> Player "Kaan" is connecting...
    >>> Player "Kaan" joined the match.
    >>> Player "Kaan" has entered the game.
    SERVER: Client closed by server...
    >>> Player "Kaan" disconnected.
    -------------------------------------------------------------------------
      G A M E   R E S U L T S
    -------------------------------------------------------------------------
       Location  : Utah
       Season    : Pre-Rut
       Time      : 04/12/12 15:22:23
       Duration  : 15 minutes
       Game type : Best_Trophy
       Difficulty: Expert
       Result    : Time_limit_hit.
       Players   : 0
    -------------------------------------------------------------------------
    SERVER: New client joined this server...
    >>> Player "jeff" is connecting...
    >>> Player "jeff" joined the match.
    >>> Game started.
    [ERROR] *** Hunter initialization was rejected from player "jeff"!!!
    >>> Player "jeff" disconnected.
    SERVER: Client closed by server...
    -------------------------------------------------------------------------
      G A M E   R E S U L T S
    -------------------------------------------------------------------------
       Location  : Utah
       Season    : Pre-Rut
       Time      : 04/12/12 15:50:09
       Duration  : 15 minutes
       Game type : Best_Trophy
       Difficulty: Expert
       Result    : Time_limit_hit.
       Players   : 0
    -------------------------------------------------------------------------
    log file goes like this.

  18. #18
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Getting specified text from text file

    That error comes from a line like this then:
    Code:
    Players   : 0
    As it doesn't contain any quotes.

    Modify the check line:
    Code:
    if (ln.Contains("Player"))
    Code:
    if (ln.Contains("Player") && ln.Contains((char)34))
    something like that... Or your line would work, initially this is what I would have done as I didn't know you only wanted to retrieve the lines that state when they entered a match.

    Code:
    if (ln.Contains(@""" joined the match."))
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  19. #19

    Thread Starter
    Lively Member
    Join Date
    Dec 2011
    Posts
    115

    Re: Getting specified text from text file

    changed to : if (ln.Contains(">>> Player")) works good now thanks. But then a connection has 3 parts like :
    1. is connecting...
    2. joined the match.
    3. has entered the game.

    we need to store only 2.

    this way it list s only the connected people : if (ln.Contains(" joined the match."))

  20. #20

    Thread Starter
    Lively Member
    Join Date
    Dec 2011
    Posts
    115

    Re: Getting specified text from text file

    I can see dublicates also , same name twice when user was disconnected and connected back for example. it doesnt check for dublicates and list only online users.

  21. #21
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Getting specified text from text file

    Quote Originally Posted by deve View Post
    I can see dublicates also , same name twice when user was disconnected and connected back for example. it doesnt check for dublicates and list only online users.
    Because you're appending to the text, you need to clear it out before updating it. Also, it changes to [Connected] if it finds one of these 3

    1. is connecting...
    2. joined the match.
    3. has entered the game.

    So if it finds all 3, then there will be duplicates, if you only check for "joined the match." and clear the text field whenever you update it, you won't see duplicates, otherwise if this file does have duplicates, then you could create an array and get distinct items or use LINQ.
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  22. #22

    Thread Starter
    Lively Member
    Join Date
    Dec 2011
    Posts
    115

    Re: Getting specified text from text file

    well I have it on a button click for now and the listbox1 is empty when Ipress the button I get some dublicates and some people who are not online.

  23. #23

    Thread Starter
    Lively Member
    Join Date
    Dec 2011
    Posts
    115

    Re: Getting specified text from text file

    between Iuse your code like this :

    Code:
        using (StreamReader sr = new StreamReader(textBox1.Text + @"\Game\Game.LOG"))
        {
            while (sr.Peek() > -1)
            {
                string ln = sr.ReadLine();
                if (ln.Contains(" joined the match."))
                {
                    string values = (from Match m in Regex.Matches(ln, @"""(.*?)""", RegexOptions.IgnoreCase)
                                     select m.Value).ToArray()[0];
    
                    //string status = ln.Contains("disco") ? "" : "";
                    listBox1.Items.Add(values.Replace((char)34, '\n')  + Environment.NewLine);
                }
            }
        }

  24. #24
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Getting specified text from text file

    Code:
    listBox1.Items.Clear();
    At the top above "using".
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  25. #25

    Thread Starter
    Lively Member
    Join Date
    Dec 2011
    Posts
    115

    Re: Getting specified text from text file

    Quote Originally Posted by AceInfinity View Post
    Code:
    listBox1.Items.Clear();
    At the top above "using".
    now there are 3 people connected and it shows on our List 20 and some of them dublicated

  26. #26
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Getting specified text from text file

    Can you provide some screenshots of what you mean? I have no idea honestly for what you're talking about then. And you are using the code in this post right? http://www.vbforums.com/showpost.php...8&postcount=23

    Use this bit of code to remove duplicates:
    Code:
    string[] arr = textBox1.Lines;
    textBox1.Text = String.Join(Environment.NewLine, arr.Distinct().ToArray());
    Last edited by AceInfinity; Apr 12th, 2012 at 02:38 PM.
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  27. #27

    Thread Starter
    Lively Member
    Join Date
    Dec 2011
    Posts
    115

    Re: Getting specified text from text file

    Quote Originally Posted by AceInfinity View Post
    Can you provide some screenshots of what you mean? I have no idea honestly for what you're talking about then. And you are using the code in this post right? http://www.vbforums.com/showpost.php...8&postcount=23

    yes I use that code here is screenshot 1 from the tool :




    and screenshot that shows how many user connected actually in the server.


  28. #28
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Getting specified text from text file

    Try this:

    vb Code:
    1. listBox1.Items.Clear();
    2.  
    3. using (StreamReader sr = new StreamReader(textBox1.Text + @"\Game\Game.LOG"))
    4. {
    5.     while (sr.Peek() > -1)
    6.     {
    7.         string ln = sr.ReadLine();
    8.         if (ln.Contains(" joined the match."))
    9.         {
    10.             string values = (from Match m in Regex.Matches(ln, @"""(.*?)""", RegexOptions.IgnoreCase)
    11.                              select m.Value).ToArray()[0];
    12.  
    13.             //string status = ln.Contains("disco") ? "" : "";
    14.             textBox1.Text += values.Replace((char)34, '\n')  + Environment.NewLine;
    15.         }
    16.     }
    17. }
    18.  
    19. string[] arr = textBox1.Lines;
    20. textBox1.Text = String.Join(Environment.NewLine, arr.Distinct().ToArray());

    This will remove all the duplicates. Assuming that your display "2/16" is correct. I'm not sure how your validating that.
    Last edited by AceInfinity; Apr 12th, 2012 at 03:20 PM.
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  29. #29

    Thread Starter
    Lively Member
    Join Date
    Dec 2011
    Posts
    115

    Re: Getting specified text from text file

    Nope I am using like this :

    Code:
    listBox1.Items.Clear();
    using (StreamReader sr = new StreamReader(textBox1.Text + @"\Game\Game.LOG"))
    {
    while (sr.Peek() > -1)
    {
    string ln = sr.ReadLine();
    string[] arr = textBox1.Lines;
    textBox1.Text = String.Join(Environment.NewLine, arr.Distinct().ToArray());
    if (ln.Contains(" joined the match."))
    {
    string values = (from Match m in Regex.Matches(ln, @"""(.*?)""", RegexOptions.IgnoreCase)
    select m.Value).ToArray()[0];
    //string status = ln.Contains("disconnected") ? " [Disconnected]" : " [Connected]";
    listBox1.Items.Add(values.Replace((char)34, '\n')  + Environment.NewLine);
    }
    is it correct to type just TextBox1.Text = coz the full url is like : textBox1.Text + @"\Game\Game.LOG" from the log file we read.

  30. #30
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Getting specified text from text file

    Quote Originally Posted by deve View Post
    Nope I am using like this :

    Code:
    listBox1.Items.Clear();
    using (StreamReader sr = new StreamReader(textBox1.Text + @"\Game\Game.LOG"))
    {
    while (sr.Peek() > -1)
    {
    string ln = sr.ReadLine();
    string[] arr = textBox1.Lines;
    textBox1.Text = String.Join(Environment.NewLine, arr.Distinct().ToArray());
    if (ln.Contains(" joined the match."))
    {
    string values = (from Match m in Regex.Matches(ln, @"""(.*?)""", RegexOptions.IgnoreCase)
    select m.Value).ToArray()[0];
    //string status = ln.Contains("disconnected") ? " [Disconnected]" : " [Connected]";
    listBox1.Items.Add(values.Replace((char)34, '\n')  + Environment.NewLine);
    }
    is it correct to type just TextBox1.Text = coz the full url is like : textBox1.Text + @"\Game\Game.LOG" from the log file we read.
    Yes it's fine, you may want to validate that location though.

    You're forgetting my Distinct function however.
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  31. #31

    Thread Starter
    Lively Member
    Join Date
    Dec 2011
    Posts
    115

    Re: Getting specified text from text file

    well I can put it outside of the loop but same efect have the dublicates and the offline users some of them listed still.

  32. #32
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Getting specified text from text file

    Quote Originally Posted by deve View Post
    well I can put it outside of the loop but same efect have the dublicates and the offline users some of them listed still.
    From the code you posted you weren't using the function to return distinct values that I posted. And make sure that these entries don't have any spaces around them, you could use the Trim() method for that.
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  33. #33

    Thread Starter
    Lively Member
    Join Date
    Dec 2011
    Posts
    115

    Re: Getting specified text from text file

    well thats how my code looks now :

    Code:
    listBox1.Items.Clear();
    using (StreamReader sr = new StreamReader(textBox1.Text + @"\Game\Game.LOG"))
    {
    while (sr.Peek() > -1)
    {
    string ln = sr.ReadLine();
    if (ln.Contains(" joined the match."))
    {
    string values = (from Match m in Regex.Matches(ln, @"""(.*?)""", RegexOptions.IgnoreCase)
    select m.Value).ToArray()[0];
    //string status = ln.Contains("disconnected") ? " [Disconnected]" : " [Connected]";
    listBox1.Items.Add(values.Replace((char)34, '\n')  + Environment.NewLine);
    string[] arr = textBox1.Lines;
    textBox1.Text = String.Join(Environment.NewLine, arr.Distinct().ToArray());
    }

  34. #34
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Getting specified text from text file



    It's working for me on the full log... So i'm not sure what you're doing wrong, but the code works.

    Edit: Do not put this in the loop though, keep it outside of the loop:
    Code:
    string[] arr = textBox1.Lines;
    textBox1.Text = String.Join(Environment.NewLine, arr.Distinct().ToArray());
    Note: You're adding items to a listview and removing duplicates from the textbox...
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  35. #35
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Getting specified text from text file

    If you're using a listbox now and decided to go that way:

    vb Code:
    1. listBox1.Items.Clear();
    2. List<string> data = new List<string>();
    3. using (StreamReader sr = new StreamReader("File.txt"))
    4. {
    5.     while (sr.Peek() > -1)
    6.     {
    7.         string ln = sr.ReadLine();
    8.         if (ln.Contains(" joined the match."))
    9.         {
    10.             string val = (from Match m in Regex.Matches(ln, @"""(.*?)""", RegexOptions.IgnoreCase)
    11.                              select m.Value).ToArray()[0];
    12.             data.Add(val.Replace((char)34, ' ').Trim());
    13.         }
    14.     }
    15. }
    16. listBox1.Items.AddRange(data.Distinct().ToArray());
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  36. #36

    Thread Starter
    Lively Member
    Join Date
    Dec 2011
    Posts
    115

    Re: Getting specified text from text file

    yes now it works it shows all the connected user from the start of log until the end but we do not have a check if user disconnected delete it from the list.

  37. #37
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Getting specified text from text file

    That's all managable with the List<string> that I created.

    Look here: http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx

    Example:
    Code:
    data.Remove("item");
    Where "item" is the data value in the list, then clear and update the listbox with this managed list. You should be able to do the rest now

    The code I posted relies on that text file, so whenever you update your listbox with THAT code, it will put whatever it parses from the textfile. So there is no check if they are disconnected, it only checks online.
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  38. #38

    Thread Starter
    Lively Member
    Join Date
    Dec 2011
    Posts
    115

    Re: Getting specified text from text file

    ok thank you very much +++

  39. #39

    Thread Starter
    Lively Member
    Join Date
    Dec 2011
    Posts
    115

    Re: Getting specified text from text file

    well I chould not figure out on how to delete the disconnected users from the link you gave me. My english isnt that good ...

  40. #40
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Getting specified text from text file

    As I mentioned, this file gets appended to everytime someone joins, so instead of it containing ONLY the data that you want to parse into the control on the form, it may contain data that used to be useful but is no longer useful because it was "old news" from statistics of a person joining, and the stuff at the end of the file is the "new news" (new data) that we actually want inside of the control.

    So there's no way to update this "view" accurately by reading from that file...

    You'll have to probably try and intercept the incoming data as it gets written to the file or try something else.
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

Page 1 of 2 12 LastLast

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