471,319 Members | 2,675 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Trouble opening a file using BufferedStream

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

Feb 22 '06 #1
3 4419
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());
}
}
}

Feb 22 '06 #2
>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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Feb 22 '06 #3
Hello cu**********@gmail.com,

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
Feb 22 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Penn Markham | last post: by
1 post views Thread by Westbrook, Christopher L (WESTBCL04) | last post: by
6 posts views Thread by Seabass | last post: by
19 posts views Thread by Lee Crabtree | last post: by
3 posts views Thread by =?Utf-8?B?S3VsZGVlcCBWaWpheWt1bWFy?= | last post: by

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.