Quantcast
Channel: Visual C# forum
Viewing all articles
Browse latest Browse all 31927

Reading text from a file as blocks

$
0
0

Hello 
I'm trying to store a text file into an array. The text should be stored separately depending on the indices as block. 
I get the error message that the index was out of range. What am I doing wrong and how it would be correct?

            int n = 0;

            FileStream stream = File.Open(path, FileMode.Open, FileAccess.ReadWrite);
            StreamReader sr = new StreamReader(stream, Encoding.Default);

            StringCollection myStr = new StringCollection();

            while (sr.ReadLine() != null)
            {
                myStr[n] += sr.ReadLine() + '\n';
                if (sr.ReadLine() == "") n++;

            }
            txtbAusgabe.Text = myStr[0];

            sr.Close();


Viewing all articles
Browse latest Browse all 31927

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>