473,657 Members | 2,598 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(f ilename);
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 7594
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("f ile.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("f ile.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(ne w FileStream(@"fi le.csv",
FileMode.Open, FileAccess.Read , FileShare.ReadW rite));

jkristia wrote:

StreamReader sr = new StreamReader("f ile.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
3359
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 the log file. Now what happens is that if i have the folder in which the stream object creates the log file open in windows explorer, i can see that it creates the log file but then it crashes, strnage , can somebody tell me how i can stop this...
1
2210
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 string = sr.ReadToEnd... My problem is that in the file (is a text file) there is some data like:
2
16373
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""", 1) This does not like the space in 'Program Files': retval = Shell("Excel.EXE " & Application.CurrentProject.Path & "\MyFile.xls", 1)
4
23031
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 method (ReadFile). I'm going to use this to edit the file paths in all of my WMP play lists. When I run this the string variable is not being changed using the replace method. Here's what I have: Private Sub ReadFile(ByVal path As String)
1
1923
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 there any more efficient way then this ?). using (StreamReader sr = new StreamReader(_fileName)) { string line; while ((line = sr.ReadLine()) != null) ...
2
1155
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
1774
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 have to open all the files with a set of extensions( .txt,.doc etc) files in a directory using only the extensions as the actual name of the files is not available ..can that be done ????
7
7757
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 FileStream("c:\HTAC10A.PRN",FileMode.Open); StreamReader inreader = new StreamReader(inFile); string line = inreader.ReadLine();
2
1981
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
8425
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8326
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8522
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6177
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4333
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2745
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1973
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1736
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.