Monday, November 25, 2013

Being smart when opening SkyDrive Smart Files in Desktop app's/ .Net

.NET Framework Blog - Opening Files from SkyDrive using .NET

Summary First;

Summary

SkyDrive for Windows 8.1 introduced a new technology, called smart files. These files can be consumed just like regular files by using Windows Runtime APIs. However, applications that uses System.IO APIs will have problems when opening a smart file unless its content is fully downloaded on the device. With this blog post, we provided you a sample code that invokes Windows.Storage APIs by reflection. This sample code will enable you to write desktop apps that operates on smart files available on Windows 8.1 and also supports running on non-Windows Runtime platforms.

Back to the top;

Since Windows 8 I fell in love with SkyDrive and use it all the time now. Windows 8.1 has improved this a lot by introducing a new concept that requires some changes to the way you interact with files. In, this post Gaye Oncul Kok, Program Manager on the .NET Framework team, explains the key things .NET developers need to know when they read and write files stored on SkyDrive.

SkyDrive for Windows 8.1 introduced a new technology, called smart files, which gives access to the files in the cloud by providing their content on demand. The technology was designed to minimize the disk space utilization on your Windows 8.1 device. You can think of the smart files as the avatars of your cloud files on a device. They have the same appearance as regular files, allowing you to browse, search and do common file operations like viewing the properties or a thumbnail of the file without downloading the full content locally. When you want to open the file, or explicitly want to make it available offline, only then are the file’s contents streamed to your device.

...

image

SkyDrive's placholder files are smaller

From a .NET developers’ perspective, if you are developing a Windows Store App or a desktop app targeting platforms that has Windows Runtime support, your app can consume smart files just like regular files by using Windows.Storage APIs. However, applications that depend on .NET Framework System.IO APIs, such as File.Open() or FileInfo.Open(), will have problems, when operating on a smart file unless its content is fully downloaded on the device. This is due to the fact that the smart files are supported in the Windows Shell layer and above, whereas System.IO APIs resides on the Win32 layer.

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 either use the related Shell APIs through COM-interop and platform invocations or provide two different implementations and distribute two versions of your app; one for Windows Runtime and the other for non-Windows Runtime platforms. There is a third option, which is provided in sample code. This code uses reflection to utilize Windows.Storage APIs, such as StorageFile.GetFileFromPathAsync and StorageFile.OpenAsync(), if the Windows Runtime types are available on the platform or falls back to System.IO APIs otherwise.

...

Sample Code to Open Smart Files in .NET Desktop Apps

Introduction

SkyDrive for Windows 8.1 introduced a new technology, called smart files, which gives access to the files in the cloud by providing their content on demand.  If you are developing a Windows Store App or a desktop app targeting platforms that has Windows Runtime support, your app can consume smart files just like regular files by using Windows.Storage APIs. However, applications that depend on .NET Framework System.IO APIs, such as File.Open() or FileInfo.Open(), will have problems, when operating on a smart file unless its content is fully downloaded on the device. This is due to the fact that the smart files are supported in the Windows Shell layer and above, whereas System.IO APIs resides on the Win32 layer.

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 to call  using reflection Windows.Storage APIs, such as StorageFile.GetFileFromPathAsync and StorageFile.OpenAsync(), if the Windows Runtime types are available on the platform or falls back to System.IO APIs otherwise.

Building the Sample

The sample contains one solution file: WindowsRuntimeLightup.sln. This solution file includes a single class library project: WindowsRuntimeLightup.csproj that targets .NET 4.5. This project can be built in VS 2012 or VS 2013. Since the calls to Windows Runtime APIs are through reflection,  no special project setting are required for using Windows Runtime APIs. After building the project, the output class library, WindowsRuntimeLightup.dll can be referenced in .NET desktop applications.

Description

This code uses reflection to utilize Windows.Storage APIs, such as StorageFile.GetFileFromPathAsync and StorageFile.OpenAsync(), if the Windows Runtime types are available on the platform or falls back to System.IO APIs otherwise.

The code provides the following public static methods in SmartFileLightUp class:

...

With this sample your app can be automagically adaptive and use the best API for your SkyDrive file access needs.

No comments: