How do I acces files on iCloud from C# console app running on macOS

Viewed 22

In an iOS app built by colleagues of mine, some files are generated and put in an iCloud container. Now I am tasked to get hold of these files from some service running on the backend. If necessary on a Mac Mini installed in our server room.

I am a C# developer so I prefer the dotnet way, which should be entirely possible with net6.0.

dotnet new console 

on My MacBook creates the Hello World! sample, which runs great on macOS.

To access files from iCloud containers, I need the NSFileManager.DefaultManager from the Foundation namespace.

This can be achieved in one of two ways AFAIK.

  1. Microsoft.MacOS by changing the <TargetFramework> from net6.0 to net6.0-macos, or

  2. Xamarin.Mac by including a reference to /Library/Frameworks/Xamarin.Mac.framework/Versions/Current/lib/64bits/full/Xamarin.Mac.dll

In either case, I need to add an Info.plist file to the project.

Using the 1st approach, the console app crashes without the console ever showing 'Hello World!'

The latter approach does show 'Hello World!', but then throws an exception:

System.ArgumentNullException: Value cannot be null.
   at System.Threading.Monitor.ReliableEnter(Object obj, Boolean& lockTaken)
   at System.Threading.Monitor.Enter(Object obj, Boolean& lockTaken)
   at ObjCRuntime.Runtime.TryGetNSObject(IntPtr ptr, Boolean evenInFinalizerQueue) in /Library/Frameworks/Xamarin.Mac.framework/Versions/8.12.0.2/src/Xamarin.Mac/ObjCRuntime/Runtime.cs:line 1374
   at ObjCRuntime.Runtime.GetNSObject[T](IntPtr ptr) in /Library/Frameworks/Xamarin.Mac.framework/Versions/8.12.0.2/src/Xamarin.Mac/ObjCRuntime/Runtime.cs:line 1426
   at Foundation.NSFileManager.get_DefaultManager() in /Library/Frameworks/Xamarin.Mac.framework/Versions/8.12.0.2/src/Xamarin.Mac/Foundation/NSFileManager.g.cs:line 1165
   at Program.<Main>$(String[] args) in /Users/sbc/Projects/Console1/Program.cs:line 8

I am new to dotnet on Mac and would rather avoid it, but if it is the only way to get to files from an iCloud container, it might be the way to go.

Here is my project file:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <RuntimeIdentifier>osx-x64</RuntimeIdentifier>
    <Selfcontained>true</Selfcontained>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <Reference Include="Xamarin.Mac.dll">
      <HintPath>/Library/Frameworks/Xamarin.Mac.framework/Versions/Current/lib/64bits/full/Xamarin.Mac.dll</HintPath>
    </Reference>
  </ItemGroup>
</Project>

Here is my Program.cs:

// See https://aka.ms/new-console-template for more information
using Foundation;

Console.WriteLine("Hello, World!");

try
{
    var manager = NSFileManager.DefaultManager;
}
catch (Exception ex)
{
    Console.WriteLine(ex.ToString());
}

Here is my Info.plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>CFBundleName</key>
    <string>.NET 6 Console Mac Sample</string>
    <key>CFBundleIdentifier</key>
    <string>com.mycompany.console1</string>
    <key>CFBundleShortVersionString</key>
    <string>1.0</string>
    <key>CFBundleVersion</key>
    <string>1</string>
    <key>LSMinimumSystemVersion</key>
    <string>12.3</string>
    <key>CFBundleDevelopmentRegion</key>
    <string>en</string>
    <key>CFBundleInfoDictionaryVersion</key>
    <string>6.0</string>
    <key>CFBundlePackageType</key>
    <string>APPL</string>
    <key>CFBundleSignature</key>
    <string>????</string>
    <key>NSHumanReadableCopyright</key>
    <string>MyCompany</string>
    <key>NSPrincipalClass</key>
    <string>NSApplication</string>
    <key>NSUbiquitousContainerIsDocumentScopePublic</key>
    <true/>
    <key>NSUbiquitousContainerName</key>
    <string>StarCommissioning</string>
    <key>NSUbiquitousContainerSupportedFolderLevels</key>
    <string>Any</string>
</dict>
</plist>
0 Answers
Related