Home > Backend Development > C++ > How to Create a Desktop Shortcut with Advanced Features using .NET?

How to Create a Desktop Shortcut with Advanced Features using .NET?

Patricia Arquette
Release: 2025-01-10 15:10:42
Original
518 people have browsed it

How to Create a Desktop Shortcut with Advanced Features using .NET?

Create desktop shortcuts with advanced features using .NET

This article describes how to use the Windows Script Host Object Model in .NET Framework 3.5 to create a shortcut on the desktop that points to a specific executable file (.EXE) and set advanced options such as hotkeys and descriptions.

Step 1: Add a reference to the Windows Script Host Object Model

  • Right-click your project in Visual Studio and select Add >Reference.
  • Navigate to the "COM" tab and select the "Windows Script Host Object Model" checkbox.

Step 2: Create shortcut

Create a shortcut using the following code snippet:

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

Code description:

  • shDesktop: Represents the desktop folder.
  • WshShell: Windows Script Host Shell object.
  • shortcutAddress: The path and file name of the shortcut.
  • IWshShortcut: Interface for creating and modifying shortcuts.
The

CreateShortcut method contains the following parameters:

  • Description: Description of the shortcut, used in tooltips.
  • Hotkey: Hotkey combination for quick access to shortcuts from the keyboard.
  • TargetPath: The path to the .EXE file pointed by the shortcut.

Other options

In addition to the above properties, you can also specify other options for the shortcut, such as:

  • Arguments: Command line parameters passed when starting the target program.
  • WindowStyle: The window style when starting the target program.
  • IconPath: Path to the custom icon used for the shortcut.
  • IconIndex: Specifies the index of the icon in the icon file.

By leveraging the Windows Script Host Object Model, you can easily create customizable desktop shortcuts in your .NET applications.

The above is the detailed content of How to Create a Desktop Shortcut with Advanced Features using .NET?. 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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template