473,659 Members | 3,277 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(File Name, FileMode.OpenOr Create,
FileAccess.Writ e, System.IO.FileS hare.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.t xt";
StreamReader sr = new StreamReader(ne w
FileStream(File Name,FileMode.O pen,FileAccess. Read));

int TotalBytesRead = 0;
int retval = sr.ReadBlock(ma pBuff, 0, mapBuff.GetLeng th(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 4061
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.goo gle.com...
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(File Name, FileMode.OpenOr Create,
FileAccess.Writ e, System.IO.FileS hare.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.t xt";
StreamReader sr = new StreamReader(ne w
FileStream(File Name,FileMode.O pen,FileAccess. Read));

int TotalBytesRead = 0;
int retval = sr.ReadBlock(ma pBuff, 0, mapBuff.GetLeng th(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(ne w
FileStream(File Name,FileMode.O pen,FileAccess. Read));

to

StreamReader sr = new StreamReader(ne w
FileStream(File Name,FileMode.O pen,FileAccess. Read,FileShare. ReadWrite));

"GB" <ga************ @yahoo.co.uk> wrote in message
news:d5******** *************** ***@posting.goo gle.com...
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(File Name, FileMode.OpenOr Create,
FileAccess.Writ e, System.IO.FileS hare.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.t xt";
StreamReader sr = new StreamReader(ne w
FileStream(File Name,FileMode.O pen,FileAccess. Read));

int TotalBytesRead = 0;
int retval = sr.ReadBlock(ma pBuff, 0, mapBuff.GetLeng th(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
2414
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 this app: =============================== static void Main(string args) { if (args.Length > 0) Console.SetIn(new StreamReader(args)); //executes if I don't use the "<", ">" redirection syntax when invoking XmlTextReader xmlin = new...
2
1634
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 Administration ID: 2
2
1706
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 as follows: private string FileShare(string strURL) { string files; DateTime datLastWriteTime; DateTime datNewLastWriteTime;
4
11515
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 specify these from the context of my application? Thanks! --
17
15541
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 information about the error and where it originated in the code. Exception Details: System.NotSupportedException: Collection is read-only.
5
1412
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 "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.
4
22932
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
1152
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 databasefile resides locally but also has tables linked that resides on a fileshare/network drive. When I try to run my app I know get an: 'database.mdb' is not a valid path. Make sure that the path name is spelled correctly and that you are...
0
1432
by: Tulgaa | last post by:
i use following code: -------------------------------------- private sub ShareFolder() Dim ct As New DirectoryEntry("WinNT://" + SystemInformation.ComputerName + "/LanmanServer") ct.AuthenticationType = AuthenticationTypes.Signing Or _ AuthenticationTypes.Sealing Or _ AuthenticationTypes.Secure ct.Username = Nothing
0
8428
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8337
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8748
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8628
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2754
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1978
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1739
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.