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

FileShare.Read error ?

GB
Hi Everybody!

I have 2 different processes/application. One is writing to a file and
another is reading from it. For some reason the code doesnt seems to
work and gives mscorlib.dll IOException error "This file is being used
by another process".
Both the applications are in C#.

P.S. Even if I try to open the file with NotePad (while my server is
writing data in the file)it gives the same error.

Here's the code esnippet :

Writing the Buffer
********************
FileStream fs = new FileStream(FileName, FileMode.OpenOrCreate,
FileAccess.Write, System.IO.FileShare.Read);

StreamWriter sw = new StreamWriter(fs);
sw.Write(buff, 0, buff.GetLength(0)); // no. of bytes to write

Reading the Buffer
*********************
string FileName = "D:\\dataFile.txt";
StreamReader sr = new StreamReader(new
FileStream(FileName,FileMode.Open,FileAccess.Read) );

int TotalBytesRead = 0;
int retval = sr.ReadBlock(mapBuff, 0, mapBuff.GetLength(0)); //bytes

if(retval > 0)
{
TotalBytesRead = TotalBytesRead + retval;

}

Thread.Sleep(50);
Can anybody point out whats the error ? or is it a BUG ?

Thanks a lot for the help,
Gaurav
Nov 16 '05 #1
2 4038
Make sure you call the Stream.Close() method when you are finished reading
and writing.
"GB" <ga************@yahoo.co.uk> wrote in message
news:d5**************************@posting.google.c om...
Hi Everybody!

I have 2 different processes/application. One is writing to a file and
another is reading from it. For some reason the code doesnt seems to
work and gives mscorlib.dll IOException error "This file is being used
by another process".
Both the applications are in C#.

P.S. Even if I try to open the file with NotePad (while my server is
writing data in the file)it gives the same error.

Here's the code esnippet :

Writing the Buffer
********************
FileStream fs = new FileStream(FileName, FileMode.OpenOrCreate,
FileAccess.Write, System.IO.FileShare.Read);

StreamWriter sw = new StreamWriter(fs);
sw.Write(buff, 0, buff.GetLength(0)); // no. of bytes to write

Reading the Buffer
*********************
string FileName = "D:\\dataFile.txt";
StreamReader sr = new StreamReader(new
FileStream(FileName,FileMode.Open,FileAccess.Read) );

int TotalBytesRead = 0;
int retval = sr.ReadBlock(mapBuff, 0, mapBuff.GetLength(0)); //bytes

if(retval > 0)
{
TotalBytesRead = TotalBytesRead + retval;

}

Thread.Sleep(50);
Can anybody point out whats the error ? or is it a BUG ?

Thanks a lot for the help,
Gaurav

Nov 16 '05 #2
When you open your stream for writing you are opening it with a file share
setting of Read. This means that trying to open it in Notepad (which is
trying to open it for reading and writing) will fail. Also, when you open
your reader stream you are using the default file share setting of Read.
This means that if your writer stream is already open for writing the
attempt to open the reader stream will fail since a handle with the
FileShare.Read setting cannot be acquired. Conversely, if your writer stream
is not open then the reader stream will open successfully but any subsequent
attempt to open the writer stream will fail.

You'll need to change this:

StreamReader sr = new StreamReader(new
FileStream(FileName,FileMode.Open,FileAccess.Read) );

to

StreamReader sr = new StreamReader(new
FileStream(FileName,FileMode.Open,FileAccess.Read, FileShare.ReadWrite));

"GB" <ga************@yahoo.co.uk> wrote in message
news:d5**************************@posting.google.c om...
Hi Everybody!

I have 2 different processes/application. One is writing to a file and
another is reading from it. For some reason the code doesnt seems to
work and gives mscorlib.dll IOException error "This file is being used
by another process".
Both the applications are in C#.

P.S. Even if I try to open the file with NotePad (while my server is
writing data in the file)it gives the same error.

Here's the code esnippet :

Writing the Buffer
********************
FileStream fs = new FileStream(FileName, FileMode.OpenOrCreate,
FileAccess.Write, System.IO.FileShare.Read);

StreamWriter sw = new StreamWriter(fs);
sw.Write(buff, 0, buff.GetLength(0)); // no. of bytes to write

Reading the Buffer
*********************
string FileName = "D:\\dataFile.txt";
StreamReader sr = new StreamReader(new
FileStream(FileName,FileMode.Open,FileAccess.Read) );

int TotalBytesRead = 0;
int retval = sr.ReadBlock(mapBuff, 0, mapBuff.GetLength(0)); //bytes

if(retval > 0)
{
TotalBytesRead = TotalBytesRead + retval;

}

Thread.Sleep(50);
Can anybody point out whats the error ? or is it a BUG ?

Thanks a lot for the help,
Gaurav

Nov 16 '05 #3

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

Similar topics

4
by: Bill Cohagan | last post by:
I'm writing a console app in c# and am encountering a strange problem. I'm trying to use redirection of the standard input stream to read input from a (xml) file. The following code snippet is from...
2
by: Eric Maino | last post by:
Setup Windows 2003 Server - Patched and up to date IIS 6.0 Front Page Server Extensions Installed Windows SharePoint Services Installed Visual Studio 2003 EA Web Sites Microsoft SharePoint...
2
by: Rosa | last post by:
Hi, I'm looking for an elegant solution on how to find the youngest file within a given directory. At the moment I'm storing all files in an array and loop through it comparing the creation date...
4
by: Adam Clauss | last post by:
I have a C# service (running as Network Service account) that needs to access a fileshare: \\machinename\some\path This file share requires me to login with certain credentials. How can I...
17
by: ronaldlee | last post by:
I have this error in Line 89. Collection is read-only. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more...
5
by: GB | last post by:
Hi ! I have 2 different processes/application. One is writing to a file and another is reading from it. For some reason the code doesnt seems to work and gives mscorlib.dll IOException error...
4
by: Peter Rothenbuecher | last post by:
Hello, when I try to compile the following code: /* This fragment of code is taken from an online tutorial */ #include<stdio.h> #include<fcntl.h> #include<stdlib.h> float bigbuff;
0
by: PÃ¥l Eilertsen | last post by:
Hi, I have a ASP.Net application that uses a MS Access database. The site works fine when all databases resides locally but on my public server I need to use a different database where the main...
0
by: Tulgaa | last post by:
i use following code: -------------------------------------- private sub ShareFolder() Dim ct As New DirectoryEntry("WinNT://" + SystemInformation.ComputerName + "/LanmanServer")...
1
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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.