1

I have created the following code to open a .pdf file from the root directory of my installed program.

private void HelpButton_Click(object sender, RoutedEventArgs e)
{
    System.Diagnostics.Process proc = new System.Diagnostics.Process();
    proc.EnableRaisingEvents = false;
    proc.StartInfo.FileName = "Help Me.pdf";
    proc.Start();
}

The problem is that the program, once installed and started from the desktop icon, will not open the .pdf file. However, when started from the actual file that the desktop shortcut is pointing to, the button opens the file just fine. I have also tried the following with the same effect.

private void HelpButton_Click(object sender, RoutedEventArgs e)
{
    ProcessStartInfo helpInfo = new ProcessStartInfo("Help Me.pdf");
    helpInfo.WorkingDirectory = Assembly.GetExecutingAssembly().ToString();
    Process.Start(helpInfo);
}

private void HelpButton_Click(object sender, RoutedEventArgs e)
{
    ProcessStartInfo helpInfo = new ProcessStartInfo("Help Me.pdf");
    Process.Start(helpInfo);
}


private void HelpButton_Click(object sender, RoutedEventArgs e)
{
    System.Diagnostics.Process.Start("Help Me.pdf");
}

Any help would be greatly appreciated

Nym Pseudo
  • 11
  • 1
  • The current directory is not what you expect. Use the full path. – SLaks Sep 17 '18 at 22:50
  • 1
    `Assembly.GetExecutingAssembly().ToString();` is not a path, let alone a directory. – SLaks Sep 17 '18 at 22:51
  • As I said, these snippets of code work when I run the program from inside program files, but not with a desktop shortcut. – Nym Pseudo Sep 17 '18 at 22:52
  • 2
    And as @SLaks said, `Assembly.GetExecutingAssembly.ToString()` does not return a path, and *the current directory is not what you think*. When you run from Program Files, the current directory is different than when you run from a Desktop shortcut, and your code to set the directory isn't setting the directory. But you can verify this yourself with a simple messagebox, can't you? – Ken White Sep 17 '18 at 23:41
  • Something like: `string PDFPath = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "Help Me.pdf");`. `Assembly.GetExecutingAssembly().CodeBase;` if you need the `file:///...` format. – Jimi Sep 17 '18 at 23:54
  • Check the duplicate thread to learn how to get the installation folder of your executable when it is running. Then use `Path.Combine` to get the full path of your PDF file as @SLaks indicated. The APIs would be happy to serve full paths like that. – Lex Li Sep 18 '18 at 02:53

0 Answers0