How do I open an already opened file with a .net StreamReader?

Posted by Jon Cage on Stack Overflow See other posts from Stack Overflow or by Jon Cage
Published on 2009-05-22T13:19:33Z Indexed on 2010/04/07 0:23 UTC
Read the original article Hit count: 442

Filed under:
|
|
|
|

I have some .csv files which I'm using as part of a test bench. I can open them and read them without any problems unless I've already got the file open in Excel in which case I get an IOException:

System.IO.IOException : The process cannot access the file 'TestData.csv' because it is being used by another process.

This is a snippet from the test bench:

using (CsvReader csv = new CsvReader(new StreamReader(new FileStream(fullFilePath, FileMode.Open, FileAccess.Read)), false))
{
    // Process the file
}

Is this a limitation of StreamReader? I can open the file in other applications (Notepad++ for example) so it can't be an O/S problem. Maybe I need to use some other class? If anyone knows how I can get round this (aside from closing excel!) I'd be very grateful.

© Stack Overflow or respective owner

Related posts about .NET

Related posts about streamreader