作者shinywong (Shiny Wong)
看板C_Sharp
标题Re: [问题] 制作shortcut遇到简体或日文就失败。
时间Sat Jan 23 13:22:41 2010
因为 WshShell 调用的都是 ANSI 的 API (xxxA), 而不是 Unicode (xxxW),
所以不支援 Unicode.
如果只是 shortcut name 的问题, 可以参考
http://www.eggheadcafe.com/software/aspnet/32139594/-cant-create-shortcut.aspx
也就是先建立一个 ASCII 的 name, 然后用 File.Move 重命名.
至于 TargetPath, 用 WshShell 是无解的.
当然建立 Unicode path 的 shortcut 方法是有的:
1. 用 ShellLinkObject
http://www.dotblogs.com.tw/larrynung/archive/2008/11/02/5847.aspx
(其中的 Shell.Application Control Method)
http://bytes.com/topic/net/answers/799758-c-regarding-interop-shell32-reference
2. 用 IShellLink
http://smdn.invisiblefulmoon.net/programming/tips/createlnk/
http://www.rsdn.ru/forum/src/3098615.flat.aspx
3. 直接写 .lnk 档案
http://www.i2s-lab.com/Papers/The_Windows_Shortcut_File_Format.pdf
我用方法 1 已经成功实现了建立 Unicode 的 shortcut, 方法 2 没试过, 看起来
代码太多... 至于方法 3, 将会很复杂, 而且是 unoffical 的, 所以不推荐.
我参考的是第二个 url
http://bytes.com/topic/net/answers/799758-c-regarding-interop-shell32-reference
不过他使用的 shortcutDirectory + shortcutName,
targetDirectory + targetName 都是错误的, 因为 directory name 一般是不含有
结束的 "\", 建议使用 Path.Combine().
另外, directory.Items().Item(shortcutName + ".lnk") 还可以使用
directory.ParseName(shortcutName + ".lnk").
还有, 我使用 link.Save(null) 总是 fail, 必须使用
link.Save(Path.Combine(shortcutDirectory, shortcutName + ".lnk") 才行.
需要注意的地方是, 首先, 就像
http://www.dotblogs.com.tw/larrynung/archive/2008/11/02/5847.aspx
上面说的, 与 WshShell 不同, ShellLinkObject 不会主动 create 档案,
所以需要手动 create.
另外, 如果代码要写的尽可能 robust, 首先如果 shortcut 档案已存在而且是
read-only, 那么在 get ShellLinkObject.Path 的时候会有 access denied
exception. 所以要确保 shortcut 档案不能 read-only.
还有, 如果 shortcut 档案已经存在, 但是是一个非法的 shortcut (比如由其他
格式的档案改变 extension 而来), 而 ShellLinkObject 似乎不能 instantiate,
只能通过 FolderItem.GetLink 而来. 在 shortcut 档案非法的情况下 (空档案
除外), FolderItem.GetLink 同样会有 exception thrown 出来, 无法继续下面的
操作. FolderItem 有一个 property: FolderItem.IsLink, 不过看起来是直接
判断 extension 是否为 .lnk. 在这种情况下, 只能先删除非法的 shortcut 档案,
然后再建立一个空 shortcut 档案.
最后, 除了建立 .lnk 档案, 还可以建立 .url 档案:
http://www.sorrowman.org/c-sharp-programmer/url-link-to-desktop.html
不过首先 .url 档案不能 Open file location, 并不是 user-friendly.
其次对于 Unicode 的处理, .url 不能使用 Unicode encoding (至少 XP 是这样,
Vista/7 未试验), 对于 Unicode 的 path, .url 档案会增加
[InternetShortcut.A] 和 [InternetShortcut.W] 这两个 section, 而且
encode 的方式个人感觉很复杂, 而且针对不同的 system locale 要分别处理,
如果 system locale 更改过, 这个 .url 很可能会无法使用 (未试验). 当然这
是指直接操作 .url 档案. 不知道 Windows 是否有可处理 Unicode path 的 .url
的 API (未作深入研究).
※ 引述《senjor (背德之哞)》之铭言:
: 以下是小弟的程式
: ========================
: WshShellClass myWshShell;
: // Create a new instance of WshShellClass
: myWshShell = new WshShellClass();
: // Create the shortcut
: IWshRuntimeLibrary.IWshShortcut MyShortcut;
: // Choose the path for the shortcut
: MyShortcut =
: (IWshRuntimeLibrary.IWshShortcut)myWshShell.CreateShortcut(name + ".lnk");
: // Where the shortcut should point to
: MyShortcut.TargetPath = filePath;
: // Create the shortcut at the given path
: MyShortcut.Save();
: =======================
: 就是FilePath的地方没有办法接受简体字或日文
: 有没有什麽方法可以让他变成Unicode呢?
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 220.166.44.185
※ 编辑: shinywong 来自: 220.166.44.185 (01/23 13:51)
1F:推 bdvstg:推 01/23 22:49
2F:推 senjor:推推 01/24 10:20