Search code examples
c#filestreamreader

C# File.Read Error: File already in Use by other process


i am trying to parse through several Files, while doing so there are some values to ignore and so on. I currently did it with this Code:

double[][] rows = File  
         .ReadLines(filepath)  
         .Select(line => line
            .Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries)  
            .Where(item => !string.Equals("NAN", item, StringComparison.OrdinalIgnoreCase)))   
         .Where(items => items.Any())
         .Select(items => items
           .Select(item => double.Parse(item, CultureInfo.InvariantCulture))  
           .ToArray())
         .ToArray();

But i recently got the Error. After searching in the Internet i found out that

 using (FileStream stream = File.Open("path to file", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
        {
            using (StreamReader reader = new StreamReader(stream))
            {
                while (!reader.EndOfStream)
                { DO SOMETHING}
            }
        }

Would fix this problem. However i can´t manage to change that Code to my needs. When i try:

      using (FileStream stream = File.Open("path to file", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
        {
            using (StreamReader reader = new StreamReader(stream))
            {
                while (!reader.EndOfStream)
                {
                    reader.ReadLine().Select(line => line
            .Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries)  
            .Where(item => !string.Equals("NAN", item, StringComparison.OrdinalIgnoreCase)))   
         .Where(items => items.Any())
         .Select(items => items
           .Select(item => double.Parse(item, CultureInfo.InvariantCulture))   
           .ToArray())
         .ToArray();
                }
            }
        }

It gives me an error on ".Split" since its not a known definition for a StreamReader... I know this maight be a really silly questions and pretty easy to solve but i can´t manage it to be done some how...

Thanks in Advance

Regards


Solution

  • Reader.ReadLine() will return a string. So when you do .Select on this you are projecting this into an IEnumerable<char>. You cannot then do .Split on a char.

    You will need to reformat your code to work by reading one line at a time

    So something line this:

    reader.ReadLine()
          .Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries)
          .Where(item => !string.Equals("NAN", item, StringComparison.OrdinalIgnoreCase))
          .Where(items => items.Any())
          .Select(items => items.Select(item => double.Parse(item.ToString())).ToArray()).ToArray();
    

    Update

    To answer your comment I would most probably change the data structure from double[][] to a List<List<double>>

    If you want to keep the double[][] you can do something like this.

    You can change the code to add to the list while streaming the file:

    List<double[]> example = new List<double[]>();
    
    while (!reader.EndOfStream)
    {
        example.Add(reader.ReadLine()
          .Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries)
          .Where(item => !string.Equals("NAN", item, StringComparison.OrdinalIgnoreCase))
          .Where(items => items.Any())
          .Select(item => double.Parse(item.ToString(), CultureInfo.InvariantCulture)).ToArray());
    }
    
    
    var returnvalue = example.ToArray();
    

    Note

    When making changes like this make sure that the results match the original code.