Create desktop shortcuts using .NET Framework 3.5 and Windows API
This article describes how to use .NET Framework 3.5 and Windows API to create a desktop shortcut that points to a specific EXE file.
First, add the COM reference "Windows Script Host Object Model" in the project.
<code class="language-csharp">using IWshRuntimeLibrary; private void CreateShortcut() { // 获取桌面文件夹路径 object shDesktop = (object)"Desktop"; // 创建新的 WshShell 对象 WshShell shell = new WshShell(); // 构造快捷方式文件路径 string shortcutAddress = (string)shell.SpecialFolders.Item(ref shDesktop) + @"\Notepad.lnk"; // 创建新的 IWshShortcut 对象 IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutAddress); // 设置快捷方式属性 shortcut.Description = "记事本的新快捷方式"; shortcut.Hotkey = "Ctrl+Shift+N"; shortcut.TargetPath = Environment.GetFolderPath(Environment.SpecialFolder.System) + @"\notepad.exe"; // 保存快捷方式 shortcut.Save(); }</code>
code saves the shortcut by calling the Save()
method. This code creates a shortcut to the Notepad application on the desktop, with additional options such as a description and hotkeys.
The above is the detailed content of How to Create Desktop Shortcuts using .NET Framework 3.5 and the Windows API?. For more information, please follow other related articles on the PHP Chinese website!