创建文件夹名称包含 unicode 字符的快捷方式 [英] Creating Shortcut where folder name is having unicode characters

查看:25
本文介绍了创建文件夹名称包含 unicode 字符的快捷方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用以下代码动态创建快捷方式.但是当文件夹名称包含泰语、希腊语等 unicode 字符时,targetPath 会抛出 Argument 异常.

I have been using the below code to create shortcuts dynamically. But the targetPath throws Argument exception when the folder name has unicode characters like Thai,greek language.

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

推荐答案

从文件系统引用 Shell32.dll,转到添加引用..."对话框的 COM 选项卡并选择名为Microsoft Shell 控件和自动化"的组件"

Reference Shell32.dll from file system, go to COM tab of the "Add ref..." dialog and select the component named "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);

这篇关于创建文件夹名称包含 unicode 字符的快捷方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆