Link to home
Start Free TrialLog in
Avatar of jazzIIIlove
jazzIIIloveFlag for Sweden

asked on

Checking a particular files locking status in C#

Hi there;

I want to check a particular file's locking status in C#.

But I am not sure of the accuracy of the code, moreover I need to find out the file is locked or not for every 5000 ms.

Here is the code, you can check for.

Kind regards.

P.S. A C++ version of this question is ongoing.

Let me enhance this; when my c# application checks for the locking, is it locking the file while checking it? A bitter question for me...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading;
using System.Runtime.InteropServices;

namespace LockerConsoleApplication2
{
    class FileManager
    {
        public string _fileName;

        private int _numberOfTries;

        private int _timeIntervalBetweenTries;

        public FileStream GetStream(FileAccess fileAccess)
        {
            var tries = 0;
            while (true)
            {
                try
                {
                    Console.WriteLine("OK");
                    return File.Open(_fileName, FileMode.Open, fileAccess, FileShare.None);                    
                }
                catch (IOException e)
                {
                    if (!IsFileLocked(e))
                        throw;
                    if (++tries > _numberOfTries)
                        throw new Exception("The file is locked too long: " + e.Message, e);
                    Thread.Sleep(_timeIntervalBetweenTries);
                }
            }
        }

        public static bool IsFileLocked(IOException exception)
        {
            int errorCode = Marshal.GetHRForException(exception) & ((1 << 16) - 1);
            return errorCode == 32 || errorCode == 33;
        }

        // other code

    }
    class Program
    {

        static void Main(string[] args)
        {
            FileManager fm = new FileManager();
            fm._fileName = @"C:\MYFILE.BIN";
            fm.GetStream(FileAccess.Read);
            FileManager.IsFileLocked(new IOException());
        }
    }
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jazzIIIlove

ASKER

So, in fact my code is a crap to check whether it's used or not.

True?
If so, any ways to determine this in C#?

Regards.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
So, the question is that how can i check this with a C# code properly?

Regards.
That depends on the question...

Which question better suits your situation?
(1) Are there any other processes using this file?
(2) Can I open this file exclusively?

The two are not quite the same.
Hi there;

I think, 1. covers 2. Is it true?
but I think my code covers only 2. Is it true?

Regards.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yep.

Regards.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Extremely good link, what about vice-versa case? Any snippets or links?

Regards.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial