-1

I am following this answer about making hyperlink work in a RichTextBox? by adding this:

private void mRichTextBox_LinkClicked (object sender, LinkClickedEventArgs e) {
    System.Diagnostics.Process.Start(e.LinkText);
}

(Actually what I'm really doing is to go to the property of the control, click on the LinkClicked action, and just put the Start() method in there.)

However when clicking the link I get this error: System.ComponentModel.Win32Exception: 'An error occurred trying to start process 'https://example.com' with working directory 'XYZ'. The system cannot find the file specified.'

Why is that?

Hossein Sabziani
  • 1
  • 2
  • 15
  • 20
Ooker
  • 1,969
  • 4
  • 28
  • 58

1 Answers1

2

If you are using .NET 6 or higher, try this:

 Process.Start( new ProcessStartInfo { FileName = e.LinkText ,  UseShellExecute = true } );
Hossein Sabziani
  • 1
  • 2
  • 15
  • 20
  • can you explain why it works? – Ooker Dec 08 '22 at 15:41
  • 1
    @Ooker `UseShellExecute` true if the shell should be used when starting the process; false if the process should be created directly from the executable file. This link has good information for you https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.processstartinfo.useshellexecute?view=net-6.0 – Hossein Sabziani Dec 08 '22 at 15:47
  • Is `ProcessStartInfo` a class or an object? It seems like to be an object for being the input of the method `Start()` and having the `{}`bracket, so why can it accept `new` before it? – Ooker Dec 09 '22 at 02:19
  • @Ooker despite this being old, it still shows up in Google at the top. The `new ... {}` notation is shorthand - it's allows you to instantiate something with a list of properties already set (aka `a = new xy(); a.prop1 = 123; a.prop2 = 345;` is the same as `a = new xy{ prop1 = 123, prop2 = 123 };`. See here: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/object-and-collection-initializers – traxx2012 Aug 29 '23 at 22:45