Skip to content
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

[Windows] Make sure that FileSystem.Current.AppDataDirectory folder exists in unpackaged #23265

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 40 additions & 8 deletions src/Essentials/src/FileSystem/FileSystem.uwp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,53 @@ namespace Microsoft.Maui.Storage
{
partial class FileSystemImplementation : IFileSystem
{
private readonly Lazy<string> _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<string> _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<Stream> PlatformOpenAppPackageFileAsync(string filename)
{
Expand Down
24 changes: 10 additions & 14 deletions src/Essentials/test/DeviceTests/Tests/FileSystem_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.";
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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()
Expand All @@ -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]
Expand Down
Loading