I have a txt file with three sections. i need to use section 1 for one function section 2 for a diffrent function and part 3 for the third function. how can i read the file from a specfied delimiter to another delimiter. so bascially i want to read what is inbetween both delimiters.

Recommended Answers

All 2 Replies

In this case I would load all the string data into a variable so you can use the StringReader class (in System.IO).

The StringReader object lets you read a text file line by line. So you could scan through the lines in search of your delimeters and only read the lines in between when you have found the appropriate start and stop reading when it finds the end.

A standard piece of code I find myself using and reusing, time and time again...

using ( FileStream fileStream = File.OpenRead(CONFIG_PATH) ) {
                Regex entryRegex = new Regex(@"^\s*(?<name>\S+?)\s*\:\s*(?<value>.+?)\s*$", RegexOptions.Compiled | RegexOptions.IgnoreCase);

                using ( StreamReader reader = new StreamReader(fileStream) ) {
                    while ( (line = reader.ReadLine()) != null ) {
                        if ( entryRegex.IsMatch(line) ) {
...
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.