Table of Contents

Client library

The ZeroInstall.Client namespace allows you to invoke Zero Install commands from within other applications.

Setup

Use Detect to find the Zero Install instance on the current system. This gives you an instance of IZeroInstallClient, which enables easy programmatic access to a subset of the Command-line interface.

var zeroInstall = ZeroInstallClient.Detect;

Most methods take Requirements describing the program. A FeedUri converts to Requirements implicitly. Create Requirements explicitly for more control, e.g., to run a specific command or restrict the versions:

var requirements = new Requirements(feedUri, command: "test")
{
    Versions = new VersionRange("..!2") // Stay on 1.x
};

Getting your own feed URI

When your app was launched by Zero Install, FeedUri tells you the feed URI it was launched from. It is null otherwise.

if (ZeroInstallEnvironment.FeedUri is {} feedUri)
    EnableUpdateMenu(feedUri);

Checking for updates

UpdateAsync checks for updates and downloads them if found. It returns true if an update was downloaded:

if (await zeroInstall.UpdateAsync(feedUri))
    NotifyUpdateDownloaded();

To check for updates without downloading them (e.g., to ask the user first) compare the version currently in the cache with the latest version available online:

var current = await zeroInstall.SelectAsync(feedUri, offline: true);
var latest = await zeroInstall.SelectAsync(feedUri, refresh: true);
if (latest.MainImplementation.Version > current.MainImplementation.Version)
    NotifyUpdateAvailable(latest.MainImplementation.Version);

offline: true selects the newest version that is already in the cache. This is not necessarily the version that is running: if an update was already downloaded but your app has not been restarted yet, current is the new version and no update is reported. Compare latest with your app's own version instead if this matters.

Updating and restarting

Combine SelectAsync, DownloadAsync and Run to let the user decide whether to install an update and then restart the app:

var current = await zeroInstall.SelectAsync(feedUri, offline: true);
var latest = await zeroInstall.SelectAsync(feedUri, refresh: true);
if (latest.MainImplementation.Version > current.MainImplementation.Version
 && AskUserToUpdate(latest.MainImplementation.Version))
{
    await zeroInstall.DownloadAsync(feedUri);
    zeroInstall.Run(feedUri);
    Environment.Exit(0);
}

Downloading before calling Run() makes the restart instant and lets your code handle download errors.
If a GUI is available, DownloadAsync() and UpdateAsync() show their progress in the system tray. Otherwise they run silently.

DownloadAsync() selects versions again rather than reusing the result of SelectAsync(). In rare cases, e.g., if a new version is published in the meantime, it may download a different version than the one you showed to the user.

Running programs

There are two ways to run a program via Zero Install:

Run GetRunStartInfo
Waits No. Fire-and-forget. You start the process yourself and can wait for it to exit and read its exit code and output.
UI Zero Install may show its own UI, e.g., download progress or questions for the user. Zero Install does not ask the user any questions.
Use to Restart your own app or open another app for the user. Run a helper tool and capture its output.

Open another app for the user:

zeroInstall.Run(new FeedUri("https://apps.0install.net/gui/vlc.xml"), arguments: [filePath]);

Run a command-line tool and capture its output:

var toolFeed = new FeedUri("https://example.com/tool.xml");

await zeroInstall.DownloadAsync(toolFeed);

var startInfo = zeroInstall.GetRunStartInfo(toolFeed, needsTerminal: true, arguments: ["--version"]);
startInfo.RedirectStandardOutput = true;
using var process = Process.Start(startInfo)!;
string output = await process.StandardOutput.ReadToEndAsync();
process.WaitForExit();

Pass needsTerminal: true for console tools. This uses 0install instead of 0install-win.
Downloading first lets your code handle download errors. Otherwise the tool is downloaded silently when the process starts, and a failed download only shows up as a non-zero exit code.
A non-zero exit code may come from either Zero Install or the tool itself.

Desktop integration

Toggle the app's auto start desktop integration:

string[] categories = [IntegrationCategories.AutoStart];
if (zeroInstall.GetIntegration(feedUri).Contains(IntegrationCategories.AutoStart))
    await zeroInstall.IntegrateAsync(feedUri, remove: categories);
else
    await zeroInstall.IntegrateAsync(feedUri, add: categories);

Integrate the app the same way a regular installer would (start menu entry, file types, etc.):

await zeroInstall.IntegrateAsync(feedUri, add: IntegrationCategories.Standard);

Passing machineWide: true applies the integration for all users. This requires admin rights. On Windows, Zero Install asks the user to elevate via UAC if your process is not running as admin.

Error handling

The client library maps 0install's exit codes to exceptions. Your code should be ready to catch:

Operations that complete without changing anything are not errors. For example, UpdateAsync() returns false if there are no updates and Remove() does nothing if the app is not integrated.

A background update check, for example, should usually just log errors instead of showing them:

try
{
    if (await zeroInstall.UpdateAsync(feedUri))
        NotifyUpdateDownloaded();
}
catch (TemporarilyUnavailableException)
{
    // Try again later
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or WebException
                              or InvalidDataException or InvalidOperationException or NotSupportedException
                              or OperationCanceledException or ExitCodeException)
{
    Log.Warn("Update check failed", ex);
}

Run and GetRunStartInfo do not map exit codes to exceptions:

  • Run() does not wait for Zero Install to exit. It only throws IOException if 0install could not be launched.
  • With GetRunStartInfo() you start the process and read its exit code yourself. It may come from either Zero Install or the application that it launched.

Both throw TemporarilyUnavailableException while Zero Install is updating itself.