nmtp 0.4.1

dotnet add package nmtp --version 0.4.1
NuGet\Install-Package nmtp -Version 0.4.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="nmtp" Version="0.4.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add nmtp --version 0.4.1
#r "nuget: nmtp, 0.4.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install nmtp as a Cake Addin
#addin nuget:?package=nmtp&version=0.4.1

// Install nmtp as a Cake Tool
#tool nuget:?package=nmtp&version=0.4.1

nmtp

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

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 icon generated by DALL-E 3.

Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net7.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.4.1 251 11/16/2023

Support Apple Silicon
     Support all platforms from single nupkg