How to parse txt file for specific information in C#

Hakan
Hakan used Ask the Experts™
on
I have a output file like below and i'd like to get some information from it.

How is it possible to get information are there any expert can help me to parse it?

I'd like to run one function and get some information like; file version, version number, version description, version build like single output.

3.0, 20180100, " ", 76

!file.version=3.0! Removing this line will make the file unreadable

!**********************************************************************
! Program Version Information
!**********************************************************************
version.number = 20180100
version.desc   = ''
version.build  = 76

!**********************************************************************
!  Some Other Blocks
!**********************************************************************
some_texts_in_here
.
.
.

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Senior Developer
Commented:
E.g.

namespace ConsoleCS
{
    using System;
    using System.IO;

    public class Program
    {
        public static void Main(string[] args)
        {
            FileVersion fileVersion = new FileVersion(@"c:\temp\data.txt");
            Console.WriteLine(fileVersion);

            Console.WriteLine("\nDone.");
            Console.ReadLine();
        }
    }

    public class FileVersion
    {
        public FileVersion(string filename)
        {
            using (StreamReader file = new StreamReader(filename))
            {
                bool build = false;
                bool description = false;
                bool number = false;
                string line;

                while ((line = file.ReadLine()) != null)
                {
                    if (!build && line.StartsWith("version.build"))
                    {
                        build = true;
                        this.Build = int.Parse(line.Substring(17));
                    }

                    if (!description && line.StartsWith("version.desc"))
                    {
                        description = true;
                        this.Description = (line.Substring(17));
                    }

                    if (!number && line.StartsWith("version.number"))
                    {
                        number = true;
                        this.Number = int.Parse(line.Substring(17));
                    }

                    if (build && description && number)
                    {
                        break;
                    }
                }
            }
        }

        public int Build { get; private set; }

        public string Description { get; private set; }

        public int Number { get; private set; }

        public override string ToString()
        {
            return $"Number: {this.Number}; Build: {this.Build}; Description: {this.Description};";
        }
    }
}

Open in new window

Author

Commented:
Hi ste5an,

You're great!. It's working good.

Author

Commented:
Hi ste5an, it's working perfect but how it could be to adopt without using substring because some users open it manually and changing number , sometimes they can add additional space etc. so it won't be turn correct result  in this case
ste5anSenior Developer

Commented:
if (!build && line.StartsWith("version.build"))
{
    build = true;
    this.Build = int.Parse(line.Substring(line.IndexOf('=') + 1));
}

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial