Search code examples
c#.netwinformscompact-frameworkwindows-ce

How to skip lines while reading in a file using compact framework 3.5


I have a Windows Form app that I have rewritten using the Compact Framework 3.5. In the original app I had a block of code that I used to read in file and skip its first 4 lines.

Here is the code block that works fine:

var openFile = File.OpenText(fullFileName);
            var fileEmpty = openFile.ReadLine();
            if (fileEmpty != null)
            {
                var lines = File.ReadAllLines(fullFileName).Skip(4); //Will skip the first 4 then rewrite the file
                openFile.Close();//Close the reading of the file
                File.WriteAllLines(fullFileName, lines); //Reopen the file to write the lines
                openFile.Close();//Close the rewriting of the file
            } 

I had to rewrite the above code in since it can't be used like so in the Compact Framework.

Here is my code:

var openFile = File.OpenText(fullFileName);
            var fileEmpty = openFile.ReadLine();
            if (fileEmpty != null)
            {
                var sb = new StringBuilder();
                using (var sr = new StreamReader(fullFileName))
                {
                    string line1;
                    // Read and display lines from the file until the end of 
                    // the file is reached.
                    while ((line1 = sr.ReadLine().Skip(4).ToString()) != null) //Will skip the first 4 then rewrite the file
                    {
                        sb.AppendLine(line1);
                    }
                }

However when I run the above, I receive the error on (while ((line1 = sr.ReadLine().Skip(4).ToString()) != null)) ArgumentNullException was unhandled and Value can not be null.

Can someone please tell me how I can do this in the compact framework?


Solution

  • Since sr.ReadLine() returns a single string, this is going to skip the first four characters in your string, return the rest as a character array, and then call ToString() on it... not what you want.

    sr.ReadLine().Skip(4).ToString()
    

    The reason you're getting an ArgumentNullException is because sr.ReadLine() eventually returns a null string, and when you try to skip the first four characters of a null string, it throws, as you can see by looking at the implementation for Skip():

    public static IEnumerable<TSource> Skip<TSource>(this IEnumerable<TSource> source, int count)
    {
        if (source == null)
            throw new ArgumentNullException("source");
    
        return SkipIterator<TSource>(source, count);
    }
    

    Keeping most of your code the same, you could just read the first few lines and do nothing with them (assuming you'll definitely have at least 4 lines in the file).

    using (var sr = new StreamReader(fullFileName))
    {
        // read the first 4 lines but do nothing with them; basically, skip them
        for (int i=0; i<4; i++)
            sr.ReadLine();
    
        string line1;
    
        while ((line1 = sr.ReadLine()) != null)
        {
            sb.AppendLine(line1);
        }
    }