jazzIIIlove
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...
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());
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
So, the question is that how can i check this with a C# code properly?
Regards.
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.
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.
ASKER
Hi there;
I think, 1. covers 2. Is it true?
but I think my code covers only 2. Is it true?
Regards.
I think, 1. covers 2. Is it true?
but I think my code covers only 2. Is it true?
Regards.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Yep.
Regards.
Regards.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Extremely good link, what about vice-versa case? Any snippets or links?
Regards.
Regards.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
True?
If so, any ways to determine this in C#?
Regards.