473,395 Members | 2,006 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,395 software developers and data experts.

Find out if other process is accessing a file?

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
9 5393
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
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
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
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
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
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
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
Unfortunately not at the moment, but perhapse in a few days.

regards

Carl
Apr 10 '07 #9
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Dan Jones | last post by:
I'm writing a script to process a directory tree of images.  In each directory, I need to process each image and generate an HTML file listing all of the images and links to the subdirectories....
2
by: Joerg Battermann | last post by:
Hello there, I was wondering how I can check whether I am allowed to read a certain file or not? Some of e.g. XP's files are not readable, not even by the Administrator, and when trying to open...
1
by: vaughn | last post by:
I'm trying to access a text file that was created by another process but I keep getting the contents of the file before the external process modified it. I believe the previous process left it...
10
by: Sorin Dolha [MCSD .NET] | last post by:
I would like to start a process from C# code as another user. The C# code is executed as the ASPNET user because it relies in a Web Page class, and I would like that the process will run as another...
1
by: Fei Yuan | last post by:
Please forgive me re-posting this question since I wasn't clear in my original post. --------> Starting an external process needs to pass it a ProcessStartInfo() object. ProcessStartInfo has a...
4
by: Chad Crowder | last post by:
I've taken a look at this article http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspnet/html/asp12282000.asp which someone posted a month or so ago regarding setting up SQL...
5
by: ZWeng | last post by:
I created a windows service using FileSystemWatcher to moniter a folder for file drops. The service uses FileStream and StreamReader to read and process the file. After it is done, the file is...
1
by: ABCL | last post by:
Hi All, I am working on the situation where 2 different Process/Application(.net) tries to open file at the same time....Or one process is updating the file and another process tries to access...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.