Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open file without (really) locking it?

Tags:

c#

.net

interop

Is it possible to open a file in a way that allows subsequent deletion/renaming of its parent folder?

I know you can do this:

File.Open("foo.bar", FileMode.Open, FileAccess.Read, FileShare.Read | FileShare.Delete)

Which will allow for the file to be deleted when the file handle is closed. However, if it does not allow the parent folder to be deleted without error.

I couldn't find anything in the framework. Have I overlooked something, or is there a native API I can interop to.

Note: I don't care if I get an exception when using the stream of the deleted file. In fact that would be ideal.

UPDATE:

So the most promising idea was the Hardlink, however I just can't make it work. I still end up with Access Denied when i try to delete the parent directory. Here is my code:

class Program
{
    [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
    static extern bool CreateHardLink(string lpFileName, string lpExistingFileName, IntPtr lpSecurityAttributes);

    static void Main(string[] args)
    {
        string hardLinkPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
        string realPath = @"C:\foo\bar.txt";
        if (CreateHardLink(hardLinkPath, realPath, IntPtr.Zero))
        {
            using (FileStream stream = File.Open(hardLinkPath, FileMode.Open, FileAccess.Read, FileShare.Delete | FileShare.ReadWrite))
            {
                Console.Write("File locked");
                Console.ReadLine();
            }

            File.Delete(hardLinkPath);
        }
        else
            Console.WriteLine("LastError:{0}", Marshal.GetLastWin32Error());
    }
}
like image 983
Greg Dean Avatar asked Mar 16 '09 01:03

Greg Dean


People also ask

How can I open a locked file?

Right-click a file (or click the ellipses (...)) to open the More Options menu. Click Lock. Choose a duration for the lock. If you choose unlimited, the file will be locked until you unlock it manually.

What causes a file to lock?

Windows locks certain types of files to prevent them from being modified simultaneously by two different users or two different applications. Normally, a file lock is engaged when the file is open, and Windows releases the lock when the application associated with the file is closed.

What is file locking in operating system?

File locking is a data management feature that restricts other users from changing a specific file. This allows only one user or process access to this file at any given time. This is to prevent the problem of interceding updates on the same files.


1 Answers

If you're working with an NTFS you can create another hardlink to the file in a temporary location, you'll avoid the file copy overhead, and the first link should still be deletable (either the file itself or a containing directory) without effecting the second.

like image 97
Eric Avatar answered Oct 13 '22 00:10

Eric