Writing/reading files

B

Bala Nagarajan

Hello,
I am trying to read a file that is being written by another process
but i always get the exception "The process cannot access the file
"filename" because it is being used by another process." How do i
simantenously read a file from c# that is written by another process? ( i
also tried to use FileShare.Read option while opening the file for reading
but with no luck!)

Also I am displaying the contents of the file in GUI as it is being writen
by another process? What is the best way to acheive this?

Thanks
Bala
 
J

Jeffrey Tan[MSFT]

Hi Bala,

Thanks for your post!

No, I do not think there is any perfect solution for this issue. This is
controlled by the first opener process. The first opener process can
specify the share mode with other processes, for example, it can specify
share read, share write or share readwrite etc... However, if your process
read operation always gets the exception "The process cannot access the
file "filename" because it is being used by another process.", it means the
opener did not specify share read mode. In Windows security area, the
security operation must obey *trust* rule. That is if the owner does not
trust you, you can not change the owner behavior, or there is a security
hole in the design.

So in our situation, the owner process does not want to share with others,
in another word, it does not turst others, so we have to obey the rule.

The best practise is handling this exception and display a user friendly
"file in use" dialog to the end user.(This is also what Windows Explorer
does)

Hope this helps!

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
C

Chris Dunaway

FileShare.Read means that you wish to allow the other process to only
Read from the file. Since the other process is writing to the file,
you need to specify at least FileShare.ReadWrite but, as Jeffery Tan
pointed out, if the other process is not sharing, then you will be out
of luck.
 
B

Bala Nagarajan

All,
Thanks a lot for the reply.

How do i montor for a changes in a file that is being written anothe
process accurately? I am using filesystemwatcher component's change event
to determine if there are any changes to the file i am monitoring. If any, i
use a streamreader's readline method to read the changes(the pointer in the
file advances everytime i read a line so when i read the next time i get the
newly written line)This seems to with 90%accurately and sometimes i seem to
miss some lines. I understand that this may not be the perfect solution as
we have no control over the firing of the filesystemwatcher's change event
and that may explain why i am missing few lines. But is there a better and
efficient way to acheive accurate changes in a file?



Thanks and i really apperciate the help.

Bala
 
J

Jeffrey Tan[MSFT]

Hi Bala,

Thanks for your feedback!

This is a different problem from original one.

I am not sure filesystemwatcher's change event will only be 90% accurate.
It internally encapsulates ReadDirectoryChangesW API to the actual work.
And I think the FileSystem driver callback to the ReadDirectoryChangesW API
to form file system notification, if filesystemwatcher is not 100%
accurate, I suspect it may be ReadDirectoryChangesW API or even the
filesystem driver design problem.

Anyway, below are some useful articles regarding how to correctly use
FileSystemWachter in .Net, you may give it a try to see if the problem lies
in your code:
"Monitoring File System using FileSystemWatcher Class - Part 1"
http://www.c-sharpcorner.com/3/FSWatcherMB.asp
"Using FileSystemWatcher"
http://www.codeguru.com/csharp/csharp/cs_network/article.php/c6043/
"FileSystemWatcher Tips "
http://weblogs.asp.net/ashben/archive/2003/10/14/31773.aspx

If you still fail to get the correct result, can you provide a detailed
steps list and a sample project to help us reproduce the problem?

Thanks

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

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