diff --git a/src/Essentials/src/FileSystem/FileSystem.uwp.cs b/src/Essentials/src/FileSystem/FileSystem.uwp.cs index 9146e54fa71d..54f480cf008f 100644 --- a/src/Essentials/src/FileSystem/FileSystem.uwp.cs +++ b/src/Essentials/src/FileSystem/FileSystem.uwp.cs @@ -9,21 +9,53 @@ namespace Microsoft.Maui.Storage { partial class FileSystemImplementation : IFileSystem { + private readonly Lazy _platformCacheDirectory = new(valueFactory: () => + { + if (AppInfoUtils.IsPackagedApp) + { + return ApplicationData.Current.LocalCacheFolder.Path; + } + else + { + string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), AppSpecificPath, "Cache"); + + if (!File.Exists(path)) + { + Directory.CreateDirectory(path); + } + + return path; + } + }); + + private readonly Lazy _platformAppDataDirectory = new(valueFactory: () => + { + if (AppInfoUtils.IsPackagedApp) + { + return ApplicationData.Current.LocalFolder.Path; + } + else + { + string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), AppSpecificPath, "Data"); + + if (!File.Exists(path)) + { + Directory.CreateDirectory(path); + } + + return path; + } + }); + static string CleanPath(string path) => string.Join("_", path.Split(Path.GetInvalidFileNameChars())); static string AppSpecificPath => Path.Combine(CleanPath(AppInfoImplementation.PublisherName), CleanPath(AppInfo.PackageName)); - string PlatformCacheDirectory - => AppInfoUtils.IsPackagedApp - ? ApplicationData.Current.LocalCacheFolder.Path - : Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), AppSpecificPath, "Cache"); + string PlatformCacheDirectory => _platformCacheDirectory.Value; - string PlatformAppDataDirectory - => AppInfoUtils.IsPackagedApp - ? ApplicationData.Current.LocalFolder.Path - : Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), AppSpecificPath, "Data"); + string PlatformAppDataDirectory => _platformAppDataDirectory.Value; Task PlatformOpenAppPackageFileAsync(string filename) { diff --git a/src/Essentials/test/DeviceTests/Tests/FileSystem_Tests.cs b/src/Essentials/test/DeviceTests/Tests/FileSystem_Tests.cs index a934f1d7d8f1..375af6c7ca2f 100644 --- a/src/Essentials/test/DeviceTests/Tests/FileSystem_Tests.cs +++ b/src/Essentials/test/DeviceTests/Tests/FileSystem_Tests.cs @@ -8,7 +8,7 @@ namespace Microsoft.Maui.Essentials.DeviceTests [Category("FileSystem")] public class FileSystem_Tests { - const string bundleFileContents = "This file was in the app bundle."; + const string BundleFileContents = "This file was in the app bundle."; [Fact] public void CacheDirectory_Is_Valid() @@ -23,23 +23,19 @@ public void AppDataDirectory_Is_Valid() } [Theory] - [InlineData("AppBundleFile.txt", bundleFileContents)] - [InlineData("AppBundleFile_NoExtension", bundleFileContents)] - [InlineData("Folder/AppBundleFile_Nested.txt", bundleFileContents)] - [InlineData("Folder\\AppBundleFile_Nested.txt", bundleFileContents)] + [InlineData("AppBundleFile.txt", BundleFileContents)] + [InlineData("AppBundleFile_NoExtension", BundleFileContents)] + [InlineData("Folder/AppBundleFile_Nested.txt", BundleFileContents)] + [InlineData("Folder\\AppBundleFile_Nested.txt", BundleFileContents)] public async Task OpenAppPackageFileAsync_Can_Load_File(string filename, string contents) { - using (var stream = await FileSystem.OpenAppPackageFileAsync(filename)) - { - Assert.NotNull(stream); + using var stream = await FileSystem.OpenAppPackageFileAsync(filename); + Assert.NotNull(stream); - using (var reader = new StreamReader(stream)) - { - var text = await reader.ReadToEndAsync(); + using var reader = new StreamReader(stream); + var text = await reader.ReadToEndAsync(); - Assert.Equal(contents, text); - } - } + Assert.Equal(contents, text); } [Fact]