By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,244 Members | 1,772 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,244 IT Pros & Developers. It's quick & easy.

Find out if other process is accessing a file?

P: n/a
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
Apr 2 '07 #1
Share this Question
Share on Google+
9 Replies


P: n/a
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

"Carl" <ca***@community.nospamwrote in message
news:OW**************@TK2MSFTNGP03.phx.gbl...
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

Apr 2 '07 #2

P: n/a
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/sys...eads/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 (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.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/subscripti...t/default.aspx.
==================================================

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

Apr 3 '07 #3

P: n/a
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

"Walter Wang [MSFT]" <wa****@online.microsoft.comwrote in message
news:oY**************@TK2MSFTNGHUB02.phx.gbl...
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/sys...eads/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 (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.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/subscripti...t/default.aspx.
==================================================

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

Apr 3 '07 #4

P: n/a
On Apr 3, 6:08 am, "Carl" <c...@community.nospamwrote:
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

"Walter Wang [MSFT]" <waw...@online.microsoft.comwrote in messagenews:oY**************@TK2MSFTNGHUB02.phx.gb l...
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/sys...AndThreads/Han...
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 (waw...@online.microsoft.com, remove 'online.')
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ps/default.asp...
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/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.
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

Apr 3 '07 #5

P: n/a
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/sys...k/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/micro...es.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 (wa****@online.microsoft.com, 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.

Apr 4 '07 #6

P: n/a
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
Apr 4 '07 #7

P: n/a
Hi Carl,

Have you got a chance to find out how the other process is using the file?
Regards,
Walter Wang (wa****@online.microsoft.com, 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.

Apr 9 '07 #8

P: n/a
Unfortunately not at the moment, but perhapse in a few days.

regards

Carl
Apr 10 '07 #9

P: n/a
Hi Carl,

Thanks for the update.

Please feel free to reply here whenever you've got updated information on
the issue.
Regards,
Walter Wang (wa****@online.microsoft.com, 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.

Apr 11 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.