Home > Backend Development > C++ > How to Create Desktop Shortcuts with .NET Framework 3.5 and the Windows API?

How to Create Desktop Shortcuts with .NET Framework 3.5 and the Windows API?

Susan Sarandon
Release: 2025-01-10 14:47:42
Original
681 people have browsed it

How to Create Desktop Shortcuts with .NET Framework 3.5 and the Windows API?

Create desktop shortcuts using .NET Framework 3.5 and Windows API

Question: How to create a desktop shortcut pointing to an EXE file using .NET Framework 3.5 and the official Windows API?

Answer:

To create a desktop shortcut with additional options such as hotkeys and description, follow these steps:

  1. Add a reference to the Windows Script Host Object Model (COM) under Project > Add Reference > COM .
  2. Import the required namespace:
using IWshRuntimeLibrary;
Copy after login
  1. Define a method to create a shortcut:
private void CreateShortcut()
{
  object shDesktop = (object)"Desktop";
  WshShell shell = new WshShell();
  string shortcutAddress = (string)shell.SpecialFolders.Item(ref shDesktop) + @"\Notepad.lnk";
}
Copy after login
  1. Create a new shortcut object:
  IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutAddress);
Copy after login
  1. Set shortcut properties:
  shortcut.Description = "记事本的新快捷方式";
  shortcut.Hotkey = "Ctrl+Shift+N";
  shortcut.TargetPath = Environment.GetFolderPath(Environment.SpecialFolder.System) + @"\notepad.exe";
Copy after login
  1. Save shortcut:
  shortcut.Save();
Copy after login

By following these steps, you can programmatically create a desktop shortcut with the desired properties using the .NET Framework 3.5 and Windows API.

The above is the detailed content of How to Create Desktop Shortcuts with .NET Framework 3.5 and the Windows API?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template