Re: [問題] 製作shortcut遇到簡體或日文就失敗。

看板C_Sharp作者 (Shiny Wong)時間14年前 (2010/01/23 13:22), 編輯推噓2(200)
留言2則, 2人參與, 最新討論串2/2 (看更多)
因為 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)

01/23 22:49, , 1F
01/23 22:49, 1F

01/24 10:20, , 2F
推推
01/24 10:20, 2F
文章代碼(AID): #1BMeUZkM (C_Sharp)
文章代碼(AID): #1BMeUZkM (C_Sharp)