Code Monkey home page Code Monkey logo

nmtp's Introduction

Nmtp

nmtp logo

nmtp is a fork of LibMtpSharp and a wrapper of libmtp with a few changes.

Get it from NuGet

Use

To list available MTP devices, create a Nmtp.RawDeviceList. For an object-oriented API, create a Nmtp.Device and use its instance methods. If you need a low-level API, use Nmtp.Native.LibMtp. The methods are mostly 1-1 with the C API. See the source or libmtp documentation for details.

Don't forget to dispose!

Platforms

The nuget packages includes native platform support for the following target frameworks:

  • win-x64
  • linux-x64
  • osx-x64
  • osx-arm64

Example

using var list = new Nmtp.RawDeviceList();
foreach(Nmtp.RawDevice device in list)
{
  Console.WriteLine(device);
}

var rawDevice = list.First();
using var device = new Nmtp.Device()
bool isOpen = device.TryOpen(ref rawDevice, cached: true);
if (!isOpen) { return; }

IEnumerable<Nmtp.Folder> folders = device.GetFolderList(storage.Id);
var cameraDir = folders.FirstOrDefault(folder => folder.Name == "Camera"); // e.g. Android Camera directory
if (cameraDir.FolderId <= 0) { return; }

// Get all PNG files from the last 7 days
List<Nmtp.File> files = device
    .GetFiles(progress =>
    {
      Log.Info($"List files progress: {progress * 100:##.#}%");
      return true; // false: cancel, true: continue
    })
    .Where(file => file.ParentId == cameraDir.FolderId)
    .Where(file => file.FileName.EndsWith(".png"))
    .Where(file => DateTime.UnixEpoch + TimeSpan.FromSeconds(file.ModificationDate) > DateTime.UtcNow - TimeSpan.FromDays(7))
    .ToList();

List<byte[]> data = files
    .Select((Nmtp.File file) =>
    {
      using var ms = new MemoryStream();
      bool ok = device.GetFile(file.ItemId, progress =>
      {
        Log.Info($"Download progress {file.FileName} {progress * 100:##.#}%");
        return false; // false: continue, true: cancel
      }, ms);

      return ms.ToArray();
    })
    .ToList();

Logo

Logo icon generated by DALL-E 3.

nmtp's People

Contributors

shaosss avatar nref avatar

Watchers

 avatar

Forkers

clockwork-muse

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.