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

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

DDD
Release: 2025-01-10 15:03:44
Original
495 people have browsed it

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

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>
Copy after login
The

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!

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template