Giter Club home page Giter Club logo

deadlock-dotnet-sdk's Introduction

deadlock-dotnet-sdk

GitHub release (latest by date) Nuget GitHub GitHub

deadlock-dotnet-sdk is a simple-to-use SDK for unlocking files in C# / dotnet on Windows based operating systems.

Usage

Add deadlock-dotnet-sdk to your solution tree using NuGet:

Install-Package deadlock-dotnet-sdk

You can initialize a new DeadLock helper object like so:

DeadLock deadLock = new DeadLock();

In addition, if you would like to rethrow inner exceptions, you can change the RethrowExceptions property when declaring a new DeadLock object:

DeadLock deadLock = new DeadLock(`true` | `false`);

You can also change the property dynamically:

deadlock.RethrowExceptions = `true` | `false`

Finding the processes that are locking a file

To find all the FileLocker objects that are locking a file, you can make use of the FindLockingProcesses method:

string path = @"C:\...\file.txt";
List<FileLocker> lockers = DeadLock.FindLockingProcesses(path);

You can also run the code asynchronously by calling the FindLockingProcessesAsync method`:

string path = @"C:\...\file.txt";
List<FileLocker> lockers = await DeadLock.FindLockingProcessesAsync(path);

To find the Process objects that are locking one or more files, you can invoke the FindLockingProcesses (or FindLockingProcessesAsync) method with multiple path parameters:

List<FileLocker> fileLockers = FindLockingProcesses("a", "c", "c");

Unlocking a file

To unlock a FileLocker, you can execute the Unlock method:

DeadLock.Unlock(locker);

You can also run the code asynchronously by running the UnlockAsync method:

await DeadLock.UnlockAsync(locker);

To unlock more than one FileLocker object, you can invoke the Unlock (or UnlockAsync) method with multiple FileLocker parameters:

Unlock(fileLockerA, fileLockerB, fileLockerC);

Alternatively, if you only want to unlock a file and you are not interested in using the FileLocker objects, you can do so by providing any of the Unlock or UnlockAsync methods with one or more string variables that represent the path or paths of the file(s) that should be unlocked:

// Unlock a single path
string path = @"C:\...\file.txt"; 
Unlock(path);

// Unlock multiple files
Unlock(path1, path2, path3);

// Asynchronously unlock one or more files
await UnlockAsync(path);
await UnlockAsync(path1, path2, path3);

FileLocker

The FileLocker object contains a List of System.Diagnostics.Process objects that are locking a file. You can retrieve the List of Process objects by retrieving the respective property:

List<Process> processes = fileLocker.Lockers;

To retrieve the path of the file that the Process objects are locking, you can make use of the Path property:

string path = fileLocker.Path;

Error handling

deadlock-dotnet-sdk has three specific Exception types that might occur when trying to find the Process objects that are locking a file.

  • RegisterResourceException
  • RmListException
  • StartSessionException

In case you want more detailed error messages, it is recommended that you call the Marshal.GetLastWin32Error method as soon as one of these Exception types occur:
https://docs.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.marshal.getlastwin32error?view=net-6.0

RegisterResourceException

This error occurs when the system goes out of memory, when a specific time-out occurs or if an invalid handle is detected. You can find more information about this exception here:
https://docs.microsoft.com/en-us/windows/win32/api/restartmanager/nf-restartmanager-rmregisterresources#return-value

RmListException

This error occurs when the system goes out of memory, when a specific time-out occurs or if an invalid handle is detected. You can find more information about this exception here:
https://docs.microsoft.com/en-us/windows/win32/api/restartmanager/nf-restartmanager-rmgetlist#return-value

StartSessionException

This error occurs when the system goes out of memory, when a specific time-out occurs or if an invalid handle is detected. You can find more information about this exception here:
https://docs.microsoft.com/en-us/windows/win32/api/restartmanager/nf-restartmanager-rmstartsession#return-value

Credits

About

This library is maintained by CodeDead. You can find more about us using the following links:

Copyright © 2022 CodeDead

deadlock-dotnet-sdk's People

Contributors

codedead avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

bintoss

deadlock-dotnet-sdk's Issues

[Feature] Unlock files by closing their handles with P/Invoke

This option can be more dangerous compared to telling a process to end and then waiting for it to end.

Personally, I prefer not closing processes that are locking a file, especially if one of them is the File Explorer desktop shell.
Instead, I typically use ProcessHacker to close the file handle.
Closing a handle held by another process is useful when that process is not responding to attempts to terminate it.

I'll be porting portions of ProcessHacker to .NET with the intention of using some of its features for other C#/.NET projects including its ability to close handles held by other processes.

I'll submit a pull request with this feature when I have a working example.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.