Find out if other process is accessing a file?

C

Carl

Hi

Is it possible to find out if another process is using a paticular file
(xml)? There does not seem to be anything useful in the File och Filestream
class for this purpose.

regards

Carl
 
K

Kevin Spencer

If you attempt to open the file and get an exception, another process is
using it. Use a try/catch block.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net
 
W

Walter Wang [MSFT]

Hi Carl,

Only kernel mode code can enumerate which files are opened by other
processes, for example, the Handle utility
(http://www.microsoft.com/technet/sysinternals/ProcessesAndThreads/Handle.ms
px) is using a driver to list all open handles by all processes.

Therefore your objective to do it in .NET might not be possible.

However, if a file is exclusively opened by other processes, an exception
will be thrown when you try to open it again in .NET.

Sincerely,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
C

Carl

Ok, Thanks for your replies.

Too bad there is no easy way. I'm waiting for a lenghy file operation to
complete, therefore it would be nice to check to see when there are no
handles (at least from the app I'm waiting for). Unfortunately the file does
not seem to be locked, by the other process (I'm only reading it), but what
saves me is that it is in XML format, so it will not be well formed if I
grab it to early.

Regards

Carl
 
C

Chris Dunaway

Ok, Thanks for your replies.

Too bad there is no easy way. I'm waiting for a lenghy file operation to
complete, therefore it would be nice to check to see when there are no
handles (at least from the app I'm waiting for). Unfortunately the file does
not seem to be locked, by the other process (I'm only reading it), but what
saves me is that it is in XML format, so it will not be well formed if I
grab it to early.

Regards

Carl

If you try to open the file exclusively, you will get an exception if
it is still in use regardless of how other programs have opened it.

Chris
 
W

Walter Wang [MSFT]

I think we need to first know how the other process is writing to the xml
file: sometimes a process will append some content to a file then close it
and repeat this until all content are written to the file. If the process
is keeping the file open for writing, normally you will have to specify
FileShare.ReadWrite or FileShare.Write to open it before the other process
closes it. Since you mentioned that you're able to read the file, if you're
not specifying this FileShare flag, I suspect the other process is not
keeping the file open. You can determine this by using FileMon
(http://www.microsoft.com/technet/sysinternals/FileAndDisk/Filemon.mspx) to
monitor the other process's file activity.

If the other process it not keeping the file open for writing, then you
really don't know whether or not it finished writing to the file.


About the FileShare stuff, you can find a related thread here:

http://groups.google.com/group/microsoft.public.dotnet.languages.csharp/brow
se_thread/thread/9adcc0203e0806e5/68232ed6863c63a3?lnk=st&q=file&rnum=5#6823
2ed6863c63a3

(Just scroll down and see the last few messages)



Regards,
Walter Wang ([email protected], remove 'online.')
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

Carl

Thanks alot for your recommendations and tips. I will investigate how the
other process is using the file and take actions from that!

regards

Carl
 
W

Walter Wang [MSFT]

Hi Carl,

Have you got a chance to find out how the other process is using the file?


Regards,
Walter Wang ([email protected], remove 'online.')
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.
 
W

Walter Wang [MSFT]

Hi Carl,

Thanks for the update.

Please feel free to reply here whenever you've got updated information on
the issue.


Regards,
Walter Wang ([email protected], remove 'online.')
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