maui icon indicating copy to clipboard operation
maui copied to clipboard

BlazorWebView: Use AppContext.BaseDirectory instead of Environment.CurrentDirectory

Open simonrozsival opened this issue 1 year ago • 2 comments
trafficstars

This is a follow up to https://github.com/dotnet/maui/pull/21505/files/2d4a1bdabf24a22b8df1e32d4535ebc89f6f7308#r1557824040

In BlazorWebView in WPF and WinForms code, we're using Environment.CurrentDirectory as the app root directory in the case when the location of the entry assembly is not known. This is the case when the app is published as a single file. We should use AppContext.BaseDirectory instead.

  • https://github.com/dotnet/maui/blob/b183cae2982f7c318f796d202f5c811d4da0edd8/src/BlazorWebView/src/WindowsForms/BlazorWebView.cs#L180
  • https://github.com/dotnet/maui/blob/b183cae2982f7c318f796d202f5c811d4da0edd8/src/BlazorWebView/src/Wpf/BlazorWebView.cs#L265

simonrozsival avatar Apr 10 '24 08:04 simonrozsival

Assign it to me

PreciousNyasulu avatar Apr 10 '24 09:04 PreciousNyasulu

To repro the original problem:

  1. Build a WinForms or WPF app using BlazorWebView
  2. Publish the app as a single EXE
  3. Run the app from a different folder. That is, say the app is in c:\myapp\cool_app.exe, then in a command prompt navigate to c:\other_folder and run c:\myapp\cool_app.exe

The original bug was caused by the 'current directory' to be different from where the app is, and due to the app being a single EXE, it couldn't find its own location to locate static assets, so content such as CSS wouldn't load. With the fix in #21797, the app can always locate its files (because the new API works even in single EXE apps), so all the app's content can be found.

Eilon avatar Apr 16 '24 17:04 Eilon