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('\\', '/'));
        }
    }
  •   @sandeepchads @AzureDevOps it's not there anymore; the morning i had the feature had a banner in the top of the page that had a button "try"
  •   @sandeepchads @AzureDevOps I had the feature yesterday morning on my environment and in the evening it was gone again 😭
  •   @markscholman @windowsdev Thanks! Jij ook!
  •   Another year @windowsdev MVP #MVPBuzz https://t.co/blu1TMBO0E
  •   RT @kevintgallo: Check it out https://t.co/F9exR1pQMx
  •   RT @stanzillaz: The new Windows Terminal is now on the Store https://t.co/n8rjsg19dv #WindowsInsiders
  •   @tfsbuck maybe not that clear as i was using my phone. in the release note notification I still see the annoucement… https://t.co/tx8DDm36q8
  •   @hermitdave @JenMsft Notifications In your phone, feature in that app
  •   @hermitdave @JenMsft same for apps, surface book has notifications while surface go hasn't for example
  •   @tfsbuck Rollout sprint 152 complete? in notification I still see thee notice of the build content, while other org… https://t.co/SujHBEYP5v