-1

There are Internet Shortcuts, that you can download and when you click on them, your browser opens the link, is there any way to read the Url from the file, using c#?

C# just says the file doesn't exist, for example when I do File.Exists() and in brackets the correct directory, the outcome is always false.

GG_Owl
  • 1
  • `System.IO.File.ReadAllText("filepath")` What is your filepath? – Jeremy Thompson Jul 11 '23 at 06:44
  • 1
    Did you add the file extension? – shingo Jul 11 '23 at 06:45
  • @GG_Owl this is a very active site, if you ask a question stay arournd for a few minutes to field questions. – Jeremy Thompson Jul 11 '23 at 07:06
  • This has been answered here [https://stackoverflow.com/questions/2565885/net-read-binary-contents-of-lnk-file](https://stackoverflow.com/questions/2565885/net-read-binary-contents-of-lnk-file) – Zaid Jul 11 '23 at 07:18
  • 1
    @Zaid Nope, Internet Shortcuts (`*.url`) files are ini files with special handling from File Explorer. Quite different from a Shortcut (`*.lnk`) file, which is an internal Windows binary format that your link is useful for. – Corey Jul 11 '23 at 08:15
  • Btw I found what was wrong, just a dumb mistake on my side, sry – GG_Owl Jul 11 '23 at 08:34

1 Answers1

0

C# just says the file doesn't exist, for example when I do File.Exists() and in brackets the correct directory, the outcome is always false.

If you can see the shortcut in Windows File Explorer but you're getting false from File.Exists() then you're almost certainly using the wrong filename.

In File Explorer the file extension for Internet Shortcuts (*.url) and Shortcuts (*.lnk) are explicitly hidden, regardless of your settings. To get the full path of the file you can select it in File Explorer and then use the Copy Path option in the Home ribbon. This will give you the full path and filename of the file regardless of settings or other things that manipulate the name.

As for extracting the URL from an Internet Shortcut (*.url) file... they're simple INI files. You can read the contents of the file as a simple text file and look for the line starting URL=. Trim the start of the line and you have your URL.

For example, here's the content of an Internet Shortcut file for Stack Overflow I just created on my desktop:

[InternetShortcut]
URL=http://stackoverflow.com

Extracting the URL from that is fairly simple:

static string? GetURLFromShortcut(string shortcutFilePath)
{
    // Confirm that the file exists.
    if (!File.Exists(shortcutFilePath))
        return null;
    // Check the extension is what we expect.
    if (string.Compare(Path.GetExtension(shortcutFilePath), ".url", true) != 0)
        return null;

    // Read lines until we find the "URL=" line.
    using var reader = File.OpenText(shortcutFilePath);
    while (reader.ReadLine() is string line)
    {
        if (reader.StartsWith("URL=", StringComparison.OrdinalIgnoreCase))
            return reader[5..];
    }

    // Hit end of file, just return null.
    return null;
}
Corey
  • 15,524
  • 2
  • 35
  • 68