473,387 Members | 1,489 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Unable to open a file using StreamReader - access exception

StreamReader throws an exception if I attempt to open a .csv file which
is also opened by Excel.
I checked this list and found what I though was the solution, first
open the file stream in read only,
But unfortunately that doesn't work either

Stream s = File.OpenRead(filename);
using (StreamReader sr = new StreamReader(s))
{
}

File.OpenRead still throws the "can not access file" exception,
however I can open the file in Notepad at the same time as it is opened
in Excel.

Is this another bug in .NET, and is there any workaround ?

Thanks
Jesper

Sep 19 '06 #1
5 7559
any idea anyone ?

Jesper

Sep 20 '06 #2
I havent looked anything up, but it sounds like excel opens a csv with
an exclusive file lock.

Try opening the file by passing in the filename into StreamReader
instead of opening a stream first from the file:

StreamReader sr = new StreamReader("file.csv");

Does that still throw an exception?

Hope this helps!

Sean

jkristia wrote:
any idea anyone ?

Jesper
Sep 20 '06 #3
>
StreamReader sr = new StreamReader("file.csv");
That was the first thing I did, but unfortunately that doesn't work
either, and that is the reason why I tried the other way.
The weird thing is that I can open the same csv file with notepad while
open in Excel with no problem.

Jesper

Sep 20 '06 #4
I found the question interesting, and couldn't figure it out myself,
which was a little crazy, but found the answer online, and it works,
but I couldn't explain to you why.

http://tinyurl.com/pw2xs

StreamReader reader = new StreamReader(new FileStream(@"file.csv",
FileMode.Open, FileAccess.Read, FileShare.ReadWrite));

jkristia wrote:

StreamReader sr = new StreamReader("file.csv");

That was the first thing I did, but unfortunately that doesn't work
either, and that is the reason why I tried the other way.
The weird thing is that I can open the same csv file with notepad while
open in Excel with no problem.

Jesper
Sep 20 '06 #5
yep - that works, thank you very much.

I think what does the trick is to set the share to ReadWrite instead of
Read.

Jesper

Sep 20 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: sameer | last post by:
Hi, My very simple application VB.NET Visual Studio 2003 console application uses a log file to write some text to it. I am using StreamWriter object to create and open and then append text to...
1
by: JM | last post by:
Hi, I have been trying to read a file, using "StreamReader" to pass the info to an string that is: ....dim stream as new FileStream(file,...) dim sr as new StreamReader(stream) dim data as...
2
by: Jason Gyetko | last post by:
I'm trying to open a file from VBA using a variable path and am not having very much luck. Can anyone help? This works: retval = Shell("""Excel.EXE"" ""C:\Program files\MyPath\MyFile.xls""",...
4
by: moondaddy | last post by:
I need to edit the text in many files so I'm writing a small routine to do this. First I have a method that loops through all the files in a directory and passes the full file path to another...
1
by: xitij9 | last post by:
Hi, I am importing a .csv file using StreamReader. I can not use Jet Engine as I cannot install it on client. I am reading it line by line and then split the line with comma and get the array. (Is...
2
by: louisphillip | last post by:
Hi, How can i read line by line from text file using stream reader or filestream in c#. Loius Phillip
3
by: mohi | last post by:
hello every one , i want to open a file and then do some processing on it and finally change its name or append something to its name and then save it back . can anyone please help! and also i...
7
by: =?Utf-8?B?TWFuanJlZSBHYXJn?= | last post by:
Hi, I am using StreamReader to read an ASCII file that contains blank lines. How can I omit reading blank lines? I tried somting like... FileStream inFile = new...
2
by: MSB80 | last post by:
How can i opem a file with filename of todays date i.e it should be able to create a text file with todays date.... pl advice Regards MSB
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.