Storage Files en Application Uri's

In Windows 8 apps kan je door middel van de StorageFile class werken met bestanden. Voor bestanden in de ApplicationData kan je ook ook gebruik maken van een application URI. Die uri ziet er als volgt uit:

ms-appdata:///local/<filename>. Dit is voor bestanden in de localfolder. Voor bestanden in de roaming folder ziet her uit als: ms-appdata:///roaming/<filename>

Via StorageFile.GetFromApplicationUri(); kan je vervolgens de storagefile ophalen. Dit kan heel handig zijn wanneer je filenames wil opslaan. Echter is er geen out of the box functionaliteit om van een storagefile naar een Uri te gaan. Deze extension method helpt je daarbij:

 

public static class StorageFileExtensions
    {
        public static Uri GetUri(this IStorageFile file)
        {
            return new Uri(string.Format("ms-appdata:///{0}", FindFilePath(file)));
        }

        private static string FindFilePath(IStorageFile file)
        {
            string pathLocalFolder = ApplicationData.Current.LocalFolder.Path;
            string pathRoamingFolder = ApplicationData.Current.RoamingFolder.Path;

            if (file.Path.StartsWith(pathLocalFolder))
            {
                return GeneratePath("local", file.Path, pathLocalFolder);
            }
            if (file.Path.StartsWith(pathRoamingFolder))
            {
                return GeneratePath("roaming", file.Path, pathLocalFolder);
            }
            throw new ArgumentException("file not compatible");
        }

        private static string GeneratePath(string type, string fullpath, string containerPath)
        {
            var path = fullpath.Substring(containerPath.Length);
            return string.Format("{0}{1}", type, path.Replace('\\', '/'));
        }
    }
  •   RT @alvinashcraft: Getting Started with Lottie-Windows. #xaml #winui #uwp #windowsdev https://t.co/P4EWilrffA
  •   RT @WindowsUI: We are releasing new samples and documentation today showing how to use the compositor's Visual Layer APIs in win32 apps: ht…
  •   RT @JenMsft: @PXAbstraction With the latest version of W10, you can now tie snipping to PrintScreen - lets you capture things that would ot…
  •   RT @JenMsft: It was so fun to talk to the MVPs today about [REDACTED] 😁 #MVPSummit #MVPBuzz
  •   RT @JenMsft: New build for Fast! Look for 18361 - let us know how it goes once you upgrade! Details here: https://t.co/KCrMGkCsCY 😊 #Window…
  •   @JenMsft Plqn to visit 33?
  •   RT @JenMsft: Image support now rolling out for Sticky Notes if you're in Skip Ahead 😊 #WindowsInsiders Details 👉🏻 https://t.co/6IAdzNYeIs…
  •   RT @ClintRutkas: having a great time discussing Windows Development topics at the #mvpSummit. If you feel a community member is a Microsof…
  •   @NicoVermeir revenge is sweet 🤣
  •   @theothernt found a whole dutch delegation at 4am @ starbucks :)