C#创建快捷方式

问题描述:

我正在尝试创建一个为所选程序创建快捷方式的应用程序。当程序启动时,它会显示listbox中的所有程序,您可以搜索该程序。如何从listbox内的选定程序创建快捷方式,并将其命名为选定的程序。我用这个代码,但我只创建了记事本的快捷方式。 Create shortcut on desktop C#C#创建快捷方式

private void CreateShortcut() 
{ 
    object shDesktop = (object)"Desktop"; 
    WshShell shell = new WshShell(); 
    //string shortcutAddress = (string)shell.SpecialFolders.Item(ref shDesktop) + @"\Notepad.lnk"; 
    string shortcutAddress = (string)shell.SpecialFolders.Item(ref shDesktop) + @"\Notepad.lnk"; 
    IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutAddress); 
    shortcut.Description = "New shortcut for a Notepad"; 
    shortcut.Hotkey = "Ctrl+Shift+N"; 
    shortcut.TargetPath = Environment.GetFolderPath(Environment.SpecialFolde r.System) + @"\notepad.exe"; 
    shortcut.Save(); 
} 
+0

你应该表明不工作 – McNets

+0

同样的代码,你可能会需要将某些参数传递给该函数,否则它将如何知道它要添加的程序? –

+0

以下是我的程序源代码:http://pastebin.com/Fy0CM0EU – Pavle

变化shortcut.TargetPath = Environment.GetFolderPath(Environment.SpecialFolde r.System) + @"\notepad.exe";shortcut.TargetPath = YourListBox.getSelected();

编辑:如果getSelected()不工作,尝试getSelectedItem()