Sample Code to Open Smart Files in .NET Desktop Apps

SkyDrive for Windows 8.1 introduced a new technology, called smart files.If you want your desktop app to run on non-Windows Runtime platforms, such as Windows 7, and also work with SkyDrive smart files available with Windows 8.1 you can use this sample code.

C# (33.0 KB)
 
 
 
 
 
(0)
331 times
Add to favorites
11/25/2013
E-mail Twitter del.icio.us Digg Facebook
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Microsoft.WindowsRuntimeLightUp
{
    internal class WindowsRuntimeStorageDelegate : WindowsRuntimeDelegateBase
    {
        #region internal fields

        /// <summary>
        /// delegate for <code>GetFileFromPathAsync</code> in <code>StorageFile</code>
        /// </summary>
        internal readonly Func<string,object> FileFromPathAsync;
        /// <summary>
        /// delegate for <code>OpenAsync</code> in <code>StorageFile</code>
        /// </summary>
        internal readonly Delegate OpenAsync;
        /// <summary>
        /// delegate for <code>AsStream</code> in <code>System.WindowsRuntimeSystemExtensions</code>
        /// </summary>
        internal readonly Delegate AsStream;
        /// <summary>
        /// delegate for <code>AsTask</code> with generic type <code>StorageFile</code> in <code>System.WindowsRuntimeSystemExtensions</code>
        /// </summary>
        internal readonly Delegate AsTaskStorageFile;
        /// <summary>
        /// delegate for <code>AsTask</code> with generic type <code>RandomAccessStream</code> in <code>System.WindowsRuntimeSystemExtensions</code>
        /// </summary>
        internal readonly Delegate AsTaskRandomAccessStream;
        #endregion

        #region private variables

        /// <summary>
        /// <code>StorageFile</code> type
        /// </summary>
        private static Type _storageFileType;
        /// <summary>
        /// <code>FileAccessMode</code> type
        /// </summary>
        private Type _fileAccessModeType;
        /// <summary>
        /// <code>RandomAccessStream</code> type
        /// </summary>
        private static Type _randomAccessStreamType;
       
        private const string AsStreamMethod = "AsStream";
        private const string GetFileFromPathAsyncMethod = "GetFileFromPathAsync";
        private const string RandomAccessStreamAssemblyQualifiedName = "Windows.Storage.Streams.IRandomAccessStream, Windows, ContentType=WindowsRuntime";
        private const string FileAccessModeAssemblyQualifiedName = "Windows.Storage.FileAccessMode, Windows, ContentType=WindowsRuntime";
        private const string OpenAsyncMethod = "OpenAsync";
        private const string RuntimeStreamExtensionsAssemblyQualifiedName = "System.IO.WindowsRuntimeStreamExtensions, System.Runtime.WindowsRuntime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
        private const string StorageFileAssemblyQualifiedName = "Windows.Storage.StorageFile, Windows, ContentType=WindowsRuntime";
       
        #endregion

        /// <summary>
        /// zero argument constructor
        /// </summary>
        public WindowsRuntimeStorageDelegate()
        {
            _storageFileType = TryInitializeWindowsRuntimeType(StorageFileAssemblyQualifiedName);
            _randomAccessStreamType = TryInitializeWindowsRuntimeType(RandomAccessStreamAssemblyQualifiedName);
            _fileAccessModeType = TryInitializeWindowsRuntimeType(FileAccessModeAssemblyQualifiedName);
            FileFromPathAsync = (Func<string,object>)InitializeFileFromPathAsync();
            OpenAsync = InitializeOpenAsync();
            AsStream = InitializeAsStream();
            
            // perform the following initializations only if System.WindowsRuntimeSystemExtensions can be accessed
            if (_systemWindowsRuntimeExtensionsType != null)
            {
                AsTaskStorageFile = InitializeAsTaskStorageFile();
                AsTaskRandomAccessStream = InitializeAsTaskRandomAccessStream();
            }
        }
        #region field initializers

        /// <summary>
        /// Initializes <code>FileFromPathAsync</code> delegate
        /// </summary>
        /// <returns><code>delegate</code> for <code>FileFromPathAsync</code></returns>
        private static Delegate InitializeFileFromPathAsync()
        {
            MethodInfo fileFromPathAsync = GetMethodInfo(_storageFileType, GetFileFromPathAsyncMethod, new[] { typeof(string) });
            return CreateDelegate(_storageFileType, fileFromPathAsync);
        }

        /// <summary>
        /// Initializes <code>OpenAsync</code> delegate
        /// </summary>
        /// <returns><code>delegate</code> for <code>OpenAsync</code></returns>
        private Delegate InitializeOpenAsync()
        {
            MethodInfo openAsync = GetMethodInfo(_storageFileType, OpenAsyncMethod, new[] { _fileAccessModeType });
            return CreateDelegate(_storageFileType, openAsync);
        }

        /// <summary>
        /// Initializes <code>AsStream</code> delegate
        /// </summary>
        /// <returns><code>delegate</code> for <code>AsStream</code> and null if type or assembly cannot be loaded</returns>
        private static Delegate InitializeAsStream()
        {
           Type runtimeStreamExtensions = TryInitializeWindowsRuntimeType(RuntimeStreamExtensionsAssemblyQualifiedName);
           MethodInfo asStream = GetMethodInfo(runtimeStreamExtensions, AsStreamMethod, new[] { _randomAccessStreamType });
           return CreateDelegate(runtimeStreamExtensions, asStream);
        }

        /// <summary>
        /// Initializes <code>AsTask</code> delegate for <code>StorageFile</code>
        /// </summary>
        /// <returns><code>delegate</code> for <code>AsTask</code></returns>
        private static Delegate InitializeAsTaskStorageFile()
        {
            return CreateDelegate(_systemWindowsRuntimeExtensionsType, _asTask.MakeGenericMethod(_storageFileType));
        }

        /// <summary>
        /// Initializes <code>AsTask</code> delegate for <code>RandomAccessStream</code>
        /// </summary>
        /// <returns><code>delegate</code> for <code>AsTask</code></returns>
        private static Delegate InitializeAsTaskRandomAccessStream()
        {
            return CreateDelegate(_systemWindowsRuntimeExtensionsType, _asTask.MakeGenericMethod(_randomAccessStreamType));
        }
        #endregion

    }
}