472,992 Members | 3,401 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,992 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 4506
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my...
1
by: Westbrook, Christopher L (WESTBCL04) | last post by:
I am having some trouble, and I can't figure out why. I'm trying to use the simple keylogger project from source forge, and then create a web interface to open the resulting files, so people from...
0
by: Parveen.Beniwal.Daffodil | last post by:
I am using the HTTPWebRequest's GET method to retrieve file from a ExchangeServer web location like "http://server/public/Mine/Minemail.EML". I am using following code for this string...
6
by: Seabass | last post by:
Hello everyone, I'm trying to do a Hex Viewer in C#. I'm reading the user selected file byte by byte but it seems to take forever ( one minute to read a larger file ). Is there any way to make...
1
by: tourist.tam | last post by:
Hi, I am trying to read a file using a BufferedStream and avoiding at maximum the use of string manipulation. I need now some help understanding on how to use byte array to hold the data and...
19
by: Lee Crabtree | last post by:
Is there a class in the framework that allows me read text from a file in an unbuffered manner? That is, I'd like to be able to read lines in the same manner as StreamReader.ReadLine(), but I also...
2
by: ARC | last post by:
Just curious if anyone is having issues with Acc 2007 once the number of objects and complexity increases? I have a fairly large app, with many linked tables, 100's of forms, queries, reports, and...
3
by: =?Utf-8?B?S3VsZGVlcCBWaWpheWt1bWFy?= | last post by:
Language: C#.NET 2.0 Technology: ASP.NET 2.0 Database: Oracle 10g Hi All, Could any one of you please suggest the BEST method to: 1. Fetch data from a very large .csv file (around 8 MB) and...
6
by: BlackLibrary | last post by:
The title probably tells it all. I'm a VB6 going to C# and trying to take a task I can do easily in VB6 and do it in C#.NET. I can really use some help and any pointers you can share. Below is...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.