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 i needed this last week!
  •   @hrist0ph sorry very busy with work at the moment; but working on a rewrite based on the new sdk but not going as fast as i want
  •   @surface thanks but says i am up to date and want all the goodness of 19H1 on my Surface Go
  •   how often i have to press check for updates to force the may update on my surface go 🐱‍👓? #windowsinsiders
  •   @JenMsft Already available to surface go?
  •   RT @windowsblog: Announcing UI Tests in CI/CD for Desktop App Developers https://t.co/3DK2wzAGlX
  •   RT @mrlacey: I get excited about the simplest (obscure?) things sometimes. Yay, XmlnsDefinitionAttribute is coming to .NET Core 3.0, which…
  •   Why does @slack starts faster on my lightweight mobile device then on my heavy powered Core i7 Laptop?
  •   RT @alexmullans: Azure Artifacts is now priced per GB stored 🥳. No more buying and assigning licenses. Start at https://t.co/JAn6daobLV. F…
  •   @LocalJoost i am the creator :) #sorry