473,320 Members | 1,829 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,320 software developers and data experts.

How do you open a file for reading like Notepad?

I was writing a program that was reading data from files (not making any
changes) and I used the File.OpenRead(filename) command. It threw an error
for one file because it said the file was being used by another process. But
I can open this file fine with Notepad and see the contents.

How can I open the file without trying to get a lock on it, since I just
want to read it? I would have imagine OpenRead would do that but it obviously
isn't.
Oct 3 '06 #1
5 6264
MrNobody,

What you want to do is use the FileStream constructor which takes a
FileAccess (with the Read value) and the FileShare value of ReadWrite (most
likely). Chances are something else is writing to the file and if you don't
specify that you can share it to write (and something is already trying to
write) then you are going to be denied access.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"MrNobody" <Mr******@discussions.microsoft.comwrote in message
news:BC**********************************@microsof t.com...
>I was writing a program that was reading data from files (not making any
changes) and I used the File.OpenRead(filename) command. It threw an error
for one file because it said the file was being used by another process.
But
I can open this file fine with Notepad and see the contents.

How can I open the file without trying to get a lock on it, since I just
want to read it? I would have imagine OpenRead would do that but it
obviously
isn't.

Oct 3 '06 #2

Nicholas,

that's exactly what I needed! Works great.

as a follow up question, assuming I used the previous method - would you
know what I could check to determine if a file is available for read to avoid
the exception from earlier ?
Oct 3 '06 #3
Maybe try opening the file with:

FileInfo.Open(name, FileMode.Open, FileAccess.Read, FileShare.Read)
MrNobody wrote:
I was writing a program that was reading data from files (not making any
changes) and I used the File.OpenRead(filename) command. It threw an error
for one file because it said the file was being used by another process. But
I can open this file fine with Notepad and see the contents.

How can I open the file without trying to get a lock on it, since I just
want to read it? I would have imagine OpenRead would do that but it obviously
isn't.
Oct 3 '06 #4
MyNobody,

Well, you could try and open the file for reading (sharing as you wish)
and catch the exception. Of course, that's not always the best idea.

You could always call the CreateFile API function through the P/Invoke
layer, and interpret the return code (it will give you an error code if you
can't open the file).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"MrNobody" <Mr******@discussions.microsoft.comwrote in message
news:C3**********************************@microsof t.com...
>
Nicholas,

that's exactly what I needed! Works great.

as a follow up question, assuming I used the previous method - would you
know what I could check to determine if a file is available for read to
avoid
the exception from earlier ?

Oct 3 '06 #5

Chris Fulstow wrote:
Maybe try opening the file with:

FileInfo.Open(name, FileMode.Open, FileAccess.Read, FileShare.Read)

That will fail if another process has opened the file for writing.
What FileShare.Read means is "Only allow other processes to Read the
file". If another process is already writing the file, you can very
well tell it *can't* write to the file!!

Oct 3 '06 #6

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

Similar topics

4
by: sam | last post by:
I have to open a files for reading, which is currently being used by another process(say file X). I could open file X in notepad, without any issues. How can I open it in vb.net? I always get an...
4
by: Jesper | last post by:
How can I open a textfile from C# using notepad (or the user assigned application for this).
7
by: Hareth | last post by:
When VS2003 is installed : I can open Form1.cs w/ windows "open with" contextmenu, then it opens the file w/ notepad & it works fine but after installing VS2005, "open with" doesnt...
2
by: preetish | last post by:
Hi, I am a total novice to VC++..I have a dialog based application developed using MFC...my requirement is to open a file (with .dat or ..txt ext) invoking Notepad from my application. The user...
3
by: RN Das | last post by:
Hi expert, I have a small web application in C# and ASP.Net and I want to open a file in notepad when clicked on a linked button. Here is hander code when clicked on the link button. It can open...
7
by: Lumpierbritches | last post by:
Thank you in advance for any and all assistance. It is greatly appreciated. I would like a command button in VB.Net 2002 to open a text file, then to allow text from a textbox on the current form...
7
by: Dmitry Akselrod | last post by:
Hello everyone, I am attempting to extract some header information from typical Microsoft Outlook MSG files in VB.NET. I am not after a complete message or attachments that may be enclosed. I...
6
by: Moumen VB.NET 2003/2005 Developer | last post by:
How can I detect if a file sitting on a network drive is still open by another application? This application resides on another machine on the network? I am using VB.NET 2003 Your help is...
6
by: =?Utf-8?B?TUNI?= | last post by:
I try to open a log file that is logging infomation by another process: StreamReader sr = File.OpenText(filePath); I got the exception message: The process cannot access the file...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.