URL中的Windows Internet快捷方式和unicode字符

时间:2013-06-13 13:08:46

标签: windows shortcuts

我有一个创建Windows Internet快捷方式文件(.url)的进程。文件以UTF-8编码。这些文件包含[InternetShortcut]部分,其中指定了URL =。在这种情况下,这些是file:///协议URL,允许人们在其LAN上打开路径。这些URL都是UNC路径。

通常情况下,此过程正常。但是当UNC路径包含Unicode字符时,例如下面的代码示例中的“í”,当最终用户尝试从Windows资源管理器中打开Internet快捷方式时,Windows无法“找到”该URL:

示例文件如下:

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

当我使用文本编辑器打开上面的示例.url文件时,我看到具有正确Unicode字符的路径。但是当我尝试从Windows资源管理器中打开文件时,为了访问该路径,Windows报告它无法访问该路径,并且它似乎破坏了Unicode字符。

创建这些快捷方式的源代码如下:

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();
    }
}

有谁知道这个问题的解决方案?

谢谢!

(我最初在超级用户上发布了这个,但我觉得这些内容更像程序员)

1 个答案:

答案 0 :(得分:1)

尝试使用等效于InternetCanonicalizeUrl的.NET,System.Uri.EscapeUriString,这样就是这样(假设你的URI在szOriginalString

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

然后将szEscapedString写为URI而不是原始。

相关问题