2016-03-27 3 views
0

J'ai utilisé le code ci-dessous pour créer des raccourcis de manière dynamique. Mais le targetPath renvoie l'exception Argument lorsque le nom du dossier comporte des caractères Unicode comme le thaï, le grec.Création d'un raccourci dans lequel le nom de dossier contient des caractères Unicode

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

Vous devrez peut-être pour créer une logique pour la chaîne de remplacement (comme u1234) pour les caractères Unicode qui ne peuvent pas être manipulés. – Ian

Répondre

1

Référence Shell32.dll du système de fichiers, allez à l'onglet COM de la « Ajouter ref ... » dialogue et sélectionnez le composant nommé « Contrôles Microsoft Shell et l'automatisation »

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); 
+1

Je suis nobugz. J'étais là. Ne faites simplement pas semblant que c'est votre code, vous devez fournir l'attribution. –