samedi 20 juin 2015

Swap dynamic amount of lines in text file

I have a lot of text files with values, although the lines in the text file should be partially scrambled.

An example of a text file is as follows:

0.00;1.2;3;2015-20-06 13:33:33
0.00;1.2;3;2015-20-06 13:33:34
0.00;1.2;3;2015-20-06 13:33:35
0.00;1.2;3;2015-20-06 13:33:36
[RAND]
0.00;1.2;3;2015-20-06 12:05:05
0.00;1.2;3;2015-20-06 12:05:22
0.00;1.2;3;2015-20-06 12:06:27
0.00;1.2;3;2015-20-06 12:05:42
[/RAND]

Everything between [RAND] and [/RAND] should be put in a random order. So far I have the following but I have absolutely no idea how to continue from here or if this is even the right approach.

using (StreamReader reader = new StreamReader(LocalFile))
{
    bool InRegion = false;
        string line;
        while ((line = reader.ReadLine()) != null)
        {
            if (line.Equals("[RAND]"))
                    InRegion = true;

                if (line.Equals("[/RAND]"))
                    InRegion = false;
        }
}

One of my concerns is that I am using StreamReader and therefor cannot change the file.

There could be 2 lines but also 10 lines inside the RAND block. Could somebody explain me how to go by this?

Thanks a lot in advance.




Aucun commentaire:

Enregistrer un commentaire