Extract block of text from text file c#

I need to open a text file, extract a block of text from it (from [TEST] to [TESTEND] and write that block to another file.
I'm pretty new to c# but I understand I need to read it with stream reader, then do some kind of split before using streamwriter to publish it, but I don't know how to split that out.

The file I will be opening will look something like this... (Obviously the words will be different)

NOCOPY
NOCOPY
NOCOPY
NOCOPY
NOCOPY
[TEST]
COPY1
COPY2
COPY3
COPY4
COPY5
[TESTEND]
NOCOPY
NOCOPY
NOCOPY
NOCOPY
NOCOPY
jhoughton7677Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SriVaddadiCommented:
Read the entire content from stream to a string and use string.SubString function to get the required string

http://msdn.microsoft.com/en-us/library/system.string.substring(VS.71).aspx
0
SriVaddadiCommented:
You can use indexof method to get the index of [Test] and [TestEnd]
0
Why Diversity in Tech Matters

Kesha Williams, certified professional and software developer, explores the imbalance of diversity in the world of technology -- especially when it comes to hiring women. She showcases ways she's making a difference through the Colors of STEM program.

jhoughton7677Author Commented:
Ok here is where I'm at so far. My only issue is I don't know how to get it to stop at [TESTEND]. What am I missing?

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Tools
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click_1(object sender, EventArgs e)
        {

            string fileopen = @"C:\Users\Josh\Desktop\TEST.ini";
            string filewrite = @"C:\Users\Josh\Desktop\Test.txt";
            StreamReader readfile = new StreamReader(fileopen);
            StreamWriter writefile = new StreamWriter(filewrite);

            string fileread = "";

            using (readfile)
            {
                fileread = readfile.ReadToEnd();
            }

            try
            {
                int i = fileread.IndexOf("[TEST]");

                string d = fileread.Substring(i);

                writefile.Write(d);

                writefile.Close();
                readfile.Close();
                MessageBox.Show("Complete!");
            }
            catch (Exception)
            {

            }
        }
    }
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jhoughton7677Author Commented:
PS. I know it looks rough, but I'm only working on the idea right now. I will clean it up later.
0
jhoughton7677Author Commented:
Nevermind I figured it out by adding

                    int i2 = fileread.IndexOf("[TESTEND]");
                    int i3 = i2 - i;

Thanks for the the help SriVaddadi.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.