1

I was just wondering how I could make a link in c# console clickable and be able to execute in a browser. Here is the code:

using System.Collections.Generic;
using System.Text;

namespace Project
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Click here to be redirected");
            Console.WriteLine("https://stackoverflow.com");

            Console.ReadLine();
        }
    }
}
Skantzy
  • 113
  • 8
  • It's not possible according to https://stackoverflow.com/questions/1246579/net-console-app-with-hyperlinks – Slaven Tojić May 20 '20 at 18:24
  • 3
    Does this answer your question? [.net console app with hyperlinks?](https://stackoverflow.com/questions/1246579/net-console-app-with-hyperlinks) – Code Stranger May 20 '20 at 18:32
  • 1
    If you are looking for DYI route - https://stackoverflow.com/questions/1944481/console-app-mouse-click-x-y-coordinate-detection-comparison – Alexei Levenkov May 20 '20 at 18:45

2 Answers2

0

This depends on the terminal emulator you use (I assume it's Command Prompt now). There is no way to change this behavior using C#.

Sotiris Panopoulos
  • 1,523
  • 1
  • 13
  • 18
  • Is there a terminal emulator that allows me to do this? – Skantzy May 20 '20 at 18:34
  • I just tested Git Bash on my machine which I had already installed and works with Ctrl+click. There is also an open issue to support this in a future update of Windows Terminal: https://github.com/microsoft/terminal/issues/204 – Sotiris Panopoulos May 20 '20 at 18:54
0

If you're using the new Windows Terminal which supports this feature, you can do it thusly:

public static string TerminalURL(string caption, string url) => $"\u001B]8;;{url}\a{caption}\u001B]8;;\a";

Console.WriteLine(TerminalURL("Google", "https://google.com"));

It won't work in the legacy command prompt, but it works great in the Windows Terminal. It's backwards compatible in such a way that a terminal that doesn't support the feature will merely show the link caption as text.

PhonicUK
  • 13,486
  • 4
  • 43
  • 62