Search code examples
c#.net-coresystem.diagnosticschmsingle-file

Can you send resources to a process in a self-contained, single file application in .NET 5?


I'm trying to include a .CHM (Microsoft Compiled HTML help) documentation file in my WPF application. It is set as a Resource in my application.

I use .NET 5 and I want to create a self-contained application using single file deployment. When I have a .CHM-file on my local machine, I can execute the file to open a default help viewer in Windows.

var p = new Process();
p.StartInfo = new ProcessStartInfo(@"D:\helpfile.chm")
{
    UseShellExecute = true
};
p.Start();

I want to do something similar with the resource I am including in my project. However, in a single file deployment there is no such thing as file paths (see https://learn.microsoft.com/en-us/dotnet/core/deploying/single-file). As I want to step "out of" the application and send the file to the process, can I send the whole file to a process out of my application?

A workaround might be to create an installer and have the help-file be installed on the user's computer, but that kind of defeats the whole point of having single file deployment.


Solution

    1. Use Application.GetResourceStream() to get the contents of your embedded CHM as a binary stream.
    2. Use System.IO.Path.GetTempPath() + Guid.NewGuid().ToString() + ".chm" to get a temporary file name, and open that file as a binary stream.
    3. Write the contents from (1) into (2), close (2).
    4. Build and launch your process using the temporary file name from (2).
    5. Optionally, delete (2) when your application exits.