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('\\', '/'));
  •   @JenMsft like the concept. but too much details to go now. disabled it
  •   RT @dotMorten: I am an Edge user, but forcing users of Windows Mail to use Edge might be the most evil thing Microsoft have done in a long…
  •   @donasarkar impressive!
  •   RT @PyImageSearch: I am excited to announce that @Microsoft + @MSFTResearch are sponsoring PyImageConf 2018 and providing FREE pre-configur…
  •   @tfsbuck thanks :)
  •   @tfsbuck never mind.. figured it out, well the creator of the PR was able to restart the merge and in second attempt it worked
  •   @terrajobst @fonssonnemans @hermitdave @imjoshfree dark is for depressed people. agree with @fonssonnemans light team ftw
  •   @tfsbuck how long should it take to complete a pull request? can that take over 15 minutes? normally pretty fast (i mean instant)
  •   RT @windowsblog: Windows 10 SDK Preview Build 17115 now available
  •   RT @csharpfritz: Visual Studio 2017 Version 15.7 Preview 1