Access the WinRT API from a desktop application

When looking at MSDN help for a Windows 8 API class, such as PasswordVault, it is clearly indicated that this class can be used from a Windows Store application or a Desktop application (Console, WinForms or WPF):

In the case of a Windows Store application, you have no references to add. Just add the specified using. For a desktop application, it's a little more complicated:

Here is the procedure for using the API from a desktop application:

  1. Edit the csproj file and add <TargetPlatformVersion>8.0</TargetPlatformVersion>

    <?xml version="1.0" encoding="utf-8"?>
    <Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <PropertyGroup>
      <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
      <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
      <ProjectGuid>{BEA97844-0370-40C1-A435-F95DC0DC0677}</ProjectGuid>
      <OutputType>Exe</OutputType>
      <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
      <TargetPlatformVersion>8.0</TargetPlatformVersion>
  2. Add a reference to Windows

  3. Add a reference to System.Runtime (C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETCore\v4.5\System.Runtime.dll)

  4. Use the API

You can now access a WinRT API class from your desktop application:

var vault = new Windows.Security.Credentials.PasswordVault();
var credentials = vault.RetrieveAll();
foreach (PasswordCredential credential in credentials)
{
    Console.WriteLine("{0} {1}", credential.Resource, credential.UserName);
}

There you go.

Enjoy this blog? Buy Me A Coffee Donate with PayPal

Leave a reply