Shan Plourde
Shan Plourde

Reputation: 8726

Windows Internet Shortcuts and unicode characters in URL

I have a process that creates Windows internet shortcut files (.url). The files are encoded in UTF-8. The files contain an [InternetShortcut] section, where a URL= is specified. In this case, these are file:/// protocol URLs, which allow people to open paths on their LAN. The URLs are all UNC paths.

Normally the process works fine. But when a UNC path contains Unicode characters, such as the "í" from the code sample below, Windows is unable to "find" the URL when an end user tries to open the internet shortcut from Windows Explorer:

A sample file follows:

[InternetShortcut]
URL=file:///\\lt-splourde\d$\POC\Montería Test\
IconIndex=1

When I open the sample .url file above with a text editor, I see the path with the proper Unicode characters. But when I try to open the file from Windows Explorer, in order to access the path, Windows reports that it is unable to access the path, and it seems to mangle the Unicode characters.

The source code that creates these shortcuts follows:

private void CreateShortcutAsUrl(string uncRootPath, string name, string path, int projectId)
{
    path = path + (path.EndsWith(@"\") ? "" : @"\");

    using (StreamWriter writer = new StreamWriter(
        String.Format(@"{0}\{1}\{2}.url", uncRootPath,
            ShortcutsDirectory, new FileServerController().SanitizeNameForDirectory(name)),
            false, Encoding.UTF8)) 
    {
        writer.WriteLine(@"[InternetShortcut]");
        writer.WriteLine(@"URL=file:///" + path);
        writer.Flush();
    }
}

Does anyone know of a solution for this issue?

Thanks!

(I had posted this on superuser originally, but I feel like the content is more programmer oriented)

Upvotes: 0

Views: 1216

Answers (1)

Roger Rowland
Roger Rowland

Reputation: 26279

Try the .NET equivalent of InternetCanonicalizeUrl, which is System.Uri.EscapeUriString, so something like this (assuming your URI is in szOriginalString

String szEscapedString = System.Uri.EscapeUriString(szOriginalString); 

Then write szEscapedString as the URI instead of the original.

Upvotes: 1

Related Questions