Solved

C# Determine if a file is being accessed

Posted on 2007-04-09
6
1,008 Views
Last Modified: 2013-11-07
How can I test to see if a file is being accessed by another process? I want to be sure that the file is not being accessed before I perform an operation on it. And I want to test for this without throwing exceptions.
0
Comment
Question by:SGyves
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 3

Expert Comment

by:JipFromParis
ID: 18878318
Just try this :

FileStream theFile = null;
try
{
    bool gotAccess = true;
    try {theFile = File.OpenFile(theFileName,  FileMode.Open, FileAccess.Read, FileShare.None);}
    catch (Exception) {gotAccess = false;}
    if (gotAccess)
   {
        // Here you've got exclusive access and you can perform the job.
   }
}
finally {if (theFile != null) {theFile.Close();}
0
 

Author Comment

by:SGyves
ID: 18878331
What if what  I want to do is Delete the file? It has to be closed. And if I got access....I am presented with the same problem of making sure that the file is closed before I attempt a delete.
0
 

Author Comment

by:SGyves
ID: 18878349
I can call Close on teh stream...but there is a period of time that must pass before the stream is actually closed and the file is released.
0
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
LVL 3

Accepted Solution

by:
JipFromParis earned 500 total points
ID: 18878361
This doesn't sound like a problem. File delete attempt would fail if the file is actually in use by any other process. In order to be more specific, may I suggest you clarify the kind of operation you intend to perform once you've got exclusive mode on the file ?
0
 

Author Comment

by:SGyves
ID: 18878389
I am making a brute force file deleter. It is all too often that I want to delete a file that I do not need any more...but it is being accessed by another process. So, I tell the program to get all processes using that file...terminate them...and delete the file in question. However, there is a delay between the killing of the process, and the deletion of the file. I need to compensate for that. I would prefer that the program attempt to delete this file until it succeeds, or times out after a certain time.
0
 

Author Comment

by:SGyves
ID: 18878679
Ok...got it. I just made a special delete method that returns false if it fails and true if it succeeds. That way  I can loop on it until it returns true.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
API v SOA 8 37
Code enhancement 4 32
Coding for the first time 9 62
Visual Studio 2015 auto inserted code 12 66
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question