-
Notifications
You must be signed in to change notification settings - Fork 2k
[Windows] Make sure that FileSystem.Current.AppDataDirectory folder exists in unpackaged
#23265
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,25 @@ namespace Microsoft.Maui.Storage | |
| { | ||
| partial class FileSystemImplementation : IFileSystem | ||
| { | ||
| private readonly string _platformAppDataDirectory; | ||
|
|
||
| public FileSystemImplementation() | ||
| { | ||
| if (AppInfoUtils.IsPackagedApp) | ||
| { | ||
| _platformAppDataDirectory = ApplicationData.Current.LocalFolder.Path; | ||
| } | ||
| else | ||
| { | ||
| _platformAppDataDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), AppSpecificPath, "Data"); | ||
|
|
||
| if (!File.Exists(_platformAppDataDirectory)) | ||
| { | ||
| Directory.CreateDirectory(_platformAppDataDirectory); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| static string CleanPath(string path) => | ||
| string.Join("_", path.Split(Path.GetInvalidFileNameChars())); | ||
|
|
||
|
|
@@ -20,10 +39,7 @@ string PlatformCacheDirectory | |
| ? ApplicationData.Current.LocalCacheFolder.Path | ||
| : Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), AppSpecificPath, "Cache"); | ||
|
|
||
| string PlatformAppDataDirectory | ||
| => AppInfoUtils.IsPackagedApp | ||
| ? ApplicationData.Current.LocalFolder.Path | ||
| : Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), AppSpecificPath, "Data"); | ||
| string PlatformAppDataDirectory => _platformAppDataDirectory; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe instead of moving the logic the constructor, it might be better to do all the checks and creating in this property? So this property will throw vs the ctor.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But then you would have some perf penalty on every access. Would that be fine or not? Ah you probably meant using Lazy. That might be a viable way indeed. |
||
|
|
||
| Task<Stream> PlatformOpenAppPackageFileAsync(string filename) | ||
| { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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."; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just code style changes in this file. |
||
|
|
||
| [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] | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is not great that this method can throw an exception in the constructor.