2017-01-24 3 views
2

J'essaie de créer une application de console qui créera un raccourci de corbeille.Créer par programme un raccourci vers la corbeille ou d'autres dossiers spéciaux

Mon code:

string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); 
object shDesktop = (object)"Desktop"; 
WshShell shell = new WshShell(); 
string shortcutAddress = (string)shell.SpecialFolders.Item(ref shDesktop) + @"\Recycle Bin.lnk"; 
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutAddress); 
shortcut.Description = "New shortcut for Recycle Bin"; 
shortcut.Hotkey = "Ctrl+Shift+N"; 
shortcut.IconLocation = @"C:\WINDOWS\System32\imageres.dll"; 
shortcut.TargetPath = Environment.GetFolderPath(Environment.SpecialFolder.System) + @"\Recycle.Bin"; 
shortcut.Save(); 

Il crée un "raccourci" mais pas du tout utilisable. Un message apparaît quand. J'essaie de l'ouvrir en produisant:

"Windows est à la recherche de recycle.bin Pour localiser vous-même votre fichier, cliquez sur" Parcourir ".

+0

Bienvenue sur Stack Overflow. S'il vous plaît remoe la [merci à l'avance] (http://meta.stackoverflow.com/questions/288160/no-thanks-damn-it) partie. –

+1

Alors qu'est-ce qui vous fait penser que la corbeille est un fichier autonome appelé 'Recycle.Bin'? Vous savez qu'il existe un paramètre dans Windows pour afficher la corbeille sur le bureau? – CodeCaster

+0

J'ai déjà ma corbeille visible mais quel est votre point? Peut-être que j'ai mal compris votre réponse .. – JustAScrubbie

Répondre

3

Indiquez le CLSID spécial de la Corbeille comme TargetPath:

IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutAddress); 
shortcut.TargetPath = "::{645ff040-5081-101b-9f08-00aa002f954e}"; 
shortcut.Save(); 

Il y a aussi pas besoin de préciser IconLocation. L'icône appropriée est choisie automatiquement dans le cas de dossiers spéciaux.

3

Si vous voulez créer un raccourci qui ouvre des dossiers spéciaux, vous devez créer un raccourci vers explorer.exe et pass the appropriate GUID préfixé par un double deux points comme argument:

string explorerExePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), "explorer.exe"); 
shortcut.TargetPath = explorerExePath; 
shortcut.Arguments = "::{645FF040-5081-101B-9F08-00AA002F954E}"; 

Vous ne même pas besoin de fournir l'explorer.exe comme cible, vous pouvez cibler le GUID directement:

shortcut.TargetPath = "::{645FF040-5081-101B-9F08-00AA002F954E}"; 

Alternativement, vous pouvez juste enable the display of the Recycle Bin on the desktop instead.

+1

Il n'y a en fait pas besoin d'utiliser un lien vers l'explorateur exe. Vous pouvez utiliser le CLSID spécial directement comme 'TargetPath'. Vois ma réponse. – NineBerry

+0

@Nine je vois, édité, merci. – CodeCaster

+0

Merci beaucoup mon pote très apprécié! – JustAScrubbie