Back to all posts

Deploying .NET MAUI Blazor App to ProgramFiles'

Posted on Mar 21, 2024

Posted in category:
Development
.NET

The most common deployment process for Windows implementations of .NET MAUI Blazor applications is to utilize the automatic MSIX package creation. However, there are situations where you may need to deploy your application as a simple exe so you can deploy it to the location of your choice or as in my case so that I could pass parameters at launch to the application.

The Issue

It sounds simple enough right? Build with "none" as your package type and you get an exe file. YOu double click it in the output directory and your application runs and nothing is wrong! The second you take that application and deploy it into a secured folder, such as C:\Program Files or similar when you launch your application you are greeted with a splash page, no real visible errors, but a totally unusable application.

The Cause

The root cause of this issue is that by default .NET MAUI Blazor creates a WebView2 folder within the application directory for temporary file storage. When you deploy into locations such as "Program Files" the application does not have the proper ability to create the needed file(s).

The Fix

You can override the storage location by setting an environment variable "WEBVIEW2_USER_DATA_FOLDER" to a location that the application would have access to. You can do this within a compiler directive to only impact the Windows application target. I added the following threee lines of code inside of my MauiProgram.cs within the CreateMauiApp method.

Additions to MauiProgram.cs
#if WINDOWS	
Environment
  .SetEnvironmentVariable("WEBVIEW2_USER_DATA_FOLDER",
     Environment.GetFolderPath(
        Environment.SpecialFolder.LocalApplicationData
     )
   );
#endif
    

In my case, I elected to use the application local data folder. However, you can choose any location that you want. Happy coding!