سؤال

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 ?

هل كانت مفيدة؟

المحلول

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).

نصائح أخرى

If you want to open .txt file in a default text editor from code,than you should reconsider putting it in the resources in the first place but put it in the same directory of your project or in the directory that is easy to retreive such as MyDocuments.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top