使用Unicode字符创建快捷方式

我正在使用IWshRuntimeLibrary用c#创建快捷方式。 快捷方式文件名是印地语“नमस्ते”。

我正在使用以下代码我的剪辑来创建快捷方式,其中shortcutName = "नमस्ते.lnk"

  WshShellClass wshShell = new WshShellClass(); IWshRuntimeLibrary.IWshShortcut shortcut; shortcut = (IWshRuntimeLibrary.IWshShortcut)wshShell.CreateShortcut(destPath + "\\" + shortcutName); shortcut.TargetPath = sourcePath; shortcut.Save(); 

shortcut.Save()我得到以下exception。

 The filename, directory name, or volume label syntax is incorrect. (Exception from HRESULT: 0x8007007B) 

你可以告诉调试器出了什么问题。 检查调试器中的“快捷方式”,并注意您的印地语名称已被问号替换。 这会产生无效的文件名并触发exception。

您正在使用一个无法处理字符串的古老脚本支持库。 你需要使用更新的东西。 Project + Add Reference,Browse选项卡,然后选择c:\ windows \ system32 \ shell32.dll。 这会将Shell32命名空间添加到您的项目中,并使用一些接口来执行与shell相关的工作。 为了实现这一目标,ShellLinkObject接口允许您修改.lnk文件的属性。 需要一个技巧,它无法从头创建新的.lnk文件。 您可以通过创建一个空的.lnk文件来解决这个问题。 这很好用:

  string destPath = @"c:\temp"; string shortcutName = @"नमस्ते.lnk"; // Create empty .lnk file string path = System.IO.Path.Combine(destPath, shortcutName); System.IO.File.WriteAllBytes(path, new byte[0]); // Create a ShellLinkObject that references the .lnk file Shell32.Shell shl = new Shell32.Shell(); Shell32.Folder dir = shl.NameSpace(destPath); Shell32.FolderItem itm = dir.Items().Item(shortcutName); Shell32.ShellLinkObject lnk = (Shell32.ShellLinkObject)itm.GetLink; // Set the .lnk file properties lnk.Path = Environment.GetFolderPath(Environment.SpecialFolder.System) + @"\notepad.exe"; lnk.Description = "nobugz was here"; lnk.Arguments = "sample.txt"; lnk.WorkingDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); lnk.Save(path);