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?
 
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
 
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
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.