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 @LocalJoost @vinothrajendran not sure if related but when i click update &amp; restart my machine restarts, bu… https://t.co/4muU3d7aGG
  •   RT @natfriedman: Today(!) we’re thrilled to announce unlimited free private repos for all GitHub users, and a new simplified Enterprise off…
  •   @rendyfaqot hi, Sorry to hear. There been some breaking change in the VSTS api... didn't had time yet on fixing tho… https://t.co/lZBMHu4CPf
  •   RT @ameijers: The Global MR Bootcamp was a blast! The biggest #mixedreality event in the world! And all made possible by you the community.…
  •   RT @LocalJoost: Attendants 'working at my Immersive Headset Development workshop at #GlobalMRBootcamp https://t.co/NaNtyO5Ocj
  •   Why do i always decide to change the whole approach of a session the evening before?
  •   @JenMsft @bing it's not working so well for high speed or the distance is a problem https://t.co/KE1kE0wQuF
  •   @danhellem @mmanela Nice!
  •   RT @danhellem: Check out the new changes for the Azure Boards work item tracking experiences in Visual Studio 2019 https://t.co/c4l0rQky1o…
  •   @LocalJoost you put a AAAA battery in your surface pro itself? or the pen?