-
Notifications
You must be signed in to change notification settings - Fork 247
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix unpackaged app debugging issue report at #406 - StorageFile.GetFi…
…leFromApplicationUriAsync doesn't support in unpackaged app (#407) * Fix StorageFile cannot load in Unpackaged App situation. * Rename the pub file, given we don't add 10 as part of win10 already in any latest sample, moreover build warning show cannot find the win-arm64.xml given the csproj define the name without 10 as well. * Update Readme.md to mention how to build or dotnet run as unpackaged app as example. * Remove the nuget.config given we don't need it, and we cannot point to public nuget feed.
- Loading branch information
1 parent
bbba82d
commit 71a9d5b
Showing
9 changed files
with
68 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 46 additions & 6 deletions
52
Samples/WindowsCopilotRuntime/cs-winui/Util/StorageFileExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,64 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
using System; | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
using Windows.Storage; | ||
using Windows.Storage.Streams; | ||
|
||
namespace WindowsCopilotRuntimeSample.Util; | ||
|
||
internal static class StorageFileExtensions | ||
{ | ||
public static async Task<string> ReadFromStorageFileAsync(this string filepath) | ||
private static bool IsPackagedApp = (Environment.GetEnvironmentVariable("PACKAGED_PRODUCT_ID") != null); | ||
|
||
internal static async Task<string> ReadTextAsync(this string filepath) | ||
{ | ||
if (filepath == null) | ||
if (string.IsNullOrWhiteSpace(filepath)) | ||
{ | ||
return string.Empty; | ||
} | ||
var uri = new Uri("ms-appx:///" + filepath); | ||
StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(uri); | ||
|
||
string content = await FileIO.ReadTextAsync(file); | ||
return content; | ||
if (IsPackagedApp) | ||
{ | ||
StorageFile file = await CreateStorageFile(filepath); | ||
string content = await FileIO.ReadTextAsync(file); | ||
return content; | ||
} | ||
else | ||
{ | ||
string filePath = CombineWithBasePath(filepath); | ||
string content = await File.ReadAllTextAsync(filePath); | ||
return content; | ||
} | ||
} | ||
|
||
internal static async Task<IRandomAccessStream> CreateStreamAsync(this string filepath) | ||
{ | ||
if (string.IsNullOrWhiteSpace(filepath)) | ||
{ | ||
return MemoryStream.Null.AsRandomAccessStream(); | ||
} | ||
|
||
if (IsPackagedApp) | ||
{ | ||
StorageFile file = await CreateStorageFile(filepath); | ||
return await file.OpenAsync(FileAccessMode.Read); | ||
} | ||
else | ||
{ | ||
string filePath = CombineWithBasePath(filepath); | ||
return File.OpenRead(filePath).AsRandomAccessStream(); | ||
} | ||
} | ||
private static Task<StorageFile> CreateStorageFile(string filepath) | ||
{ | ||
var uri = new Uri("ms-appx:///" + filepath); | ||
return StorageFile.GetFileFromApplicationUriAsync(uri).AsTask(); | ||
} | ||
|
||
private static string CombineWithBasePath(string filepath) | ||
{ | ||
return Path.Combine(AppContext.BaseDirectory, filepath); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.