Search code examples
c#.netembedded-resourceshellexecute

How to open an embedded string resource with Notepad?


I added help.txt file in Resources as text file, and did like this:

private void helpButton_Click(object sender, MouseEventArgs e)
{
    System.Diagnostics.Process.Start(myProject.Properties.Resources.help);
}

It didn't work, then I tried this way:

private void helpButton_Click(object sender, MouseEventArgs e)
{
    System.Diagnostics.Process.Start(@"help.txt");
}

and it didn't work also, but this way it works :

private void helpButton_Click(object sender, MouseEventArgs e)
    {
        System.Diagnostics.Process.Start(@"C:\Projects\CourceWork\Resources\help.txt");
    }

The problem is that I don't want to deal with this hard core defined path, what should I do ?


Solution

  • It seems you want to open an embedded resource with the default program associated with .txt files, which is usually Notepad. Unfortunatly, Notepad cannot read embedded resources. You will therefore need to extract the resource and save it as a file on disk, then open Notepad with that file.

    var helpFile = Path.Combine(Path.GetTempPath(), "help.txt");
    File.WriteAllText(helpFile, myProject.Properties.Resources.help);
    Process.Start(helpFile);
    

    Alternatively, you could create your own Form that displays the text, which would be a better solution in my opinion (unless your help file is not trivial to display, such as PDF file).