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
Susan SarandonOriginal
2025-01-10 14:47:42664browse

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:
<code class="language-csharp">using IWshRuntimeLibrary;</code>
  1. Define a method to create a shortcut:
<code class="language-csharp">private void CreateShortcut()
{
  object shDesktop = (object)"Desktop";
  WshShell shell = new WshShell();
  string shortcutAddress = (string)shell.SpecialFolders.Item(ref shDesktop) + @"\Notepad.lnk";
}</code>
  1. Create a new shortcut object:
<code class="language-csharp">  IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutAddress);</code>
  1. Set shortcut properties:
<code class="language-csharp">  shortcut.Description = "记事本的新快捷方式";
  shortcut.Hotkey = "Ctrl+Shift+N";
  shortcut.TargetPath = Environment.GetFolderPath(Environment.SpecialFolder.System) + @"\notepad.exe";</code>
  1. Save shortcut:
<code class="language-csharp">  shortcut.Save();</code>

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:
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