【问题标题】:Creating Shortcut where folder name is having unicode characters创建文件夹名称包含 Unicode 字符的快捷方式
【发布时间】:2016-03-27 05:16:08
【问题描述】:

我一直在使用下面的代码来动态创建快捷方式。但是当文件夹名称包含 unicode 字符(如泰语、希腊语)时,targetPath 会引发 Argument 异常。

IWshRuntimeLibrary.WshShell shell = new WshShell();
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutLocation);
shortcut.Description = "My shortcut description";   // The description of the shortcut
shortcut.WorkingDirectory = currentPath;


shortcut.TargetPath = targetFileLocation;                 // The path of the file that will launch when the shortcut is run
shortcut.Save();

【问题讨论】:

  • 您可能需要为无法处理的 unicode 字符创建替换字符串(如 u1234)的逻辑。

标签: c# wpf shortcut wsh lnk


【解决方案1】:

从文件系统中引用 Shell32.dll,进入“添加引用...”对话框的 COM 选项卡并选择名为“Microsoft Shell Controls And Automation”的组件

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.ShellClass();
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);

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-07-14
  • 2016-05-15
  • 1970-01-01
  • 2022-01-04
  • 2012-10-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多