Trouble opening a file using BufferedStream

C

curt.bathras

I am trying to open and read a file using the following:

BufferedStream stream = new BufferedStream(File.OpenRead(aFilename));

If the file specified by aFilename is being used by another
application, an IOException is thrown with the message: "The process
cannot access the file because it is being used by another process." I
figured since I opened the file for read only using File.OpenRead, I
would be fine... but no such luck.

The odd thing is I can open the file specified by aFilename in Notepad
with no problems. Any help would be greatly appreciated!!

Thanks,
Curt
 
Y

Yury

If you want to work with text files it is better to use StreamReader :

class Program
{
static void Main(string[] args)
{
// Just make text file
using (StreamWriter writer = new StreamWriter(@"c:\test"))
{
writer.WriteLine("test1");
writer.WriteLine("test2");
}

// No problems to read text from one file
using (StreamReader reader1 = new StreamReader(@"c:\test"))
using (StreamReader reader2 = new StreamReader(@"c:\test"))
{
Console.WriteLine("r1 " + reader1.ReadLine());
Console.WriteLine("r2 " + reader2.ReadLine());

Console.WriteLine("r1 " + reader1.ReadLine());
Console.WriteLine("r2 " + reader2.ReadLine());
}
}
}
 
M

Mattias Sjögren

BufferedStream stream = new BufferedStream(File.OpenRead(aFilename));

Are you sure you need the BufferedStream? FileStream already does
buffering.

If you use the appropriate FileStream constructor directly rather than
going via File.OpenRead you can explicitly specify the FileShare mode
you want.


Mattias
 
M

Michael Nemtsev

Hello (e-mail address removed),

The problems is that file is locked (program write smth to this file)
To open it the way notepad do it, u need to specify FileShare.ReadWrite property

FileStream fs = new FileStream(str, FileMode.Open, FileAccess.Read,
FileShare.ReadWrite);
BufferedStream stream = new BufferedStream(fs);
I am trying to open and read a file using the following:

BufferedStream stream = new BufferedStream(File.OpenRead(aFilename));

If the file specified by aFilename is being used by another
application, an IOException is thrown with the message: "The process
cannot access the file because it is being used by another process."
I figured since I opened the file for read only using File.OpenRead, I
would be fine... but no such luck.

The odd thing is I can open the file specified by aFilename in Notepad
with no problems. Any help would be greatly appreciated!!

Thanks,
Curt
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top