Home > Backend Development > C++ > How Can I Programmatically Create a Desktop Shortcut Using .NET Framework?

How Can I Programmatically Create a Desktop Shortcut Using .NET Framework?

Barbara Streisand
Release: 2025-01-10 15:13:46
Original
564 people have browsed it

How Can I Programmatically Create a Desktop Shortcut Using .NET Framework?

Use .NET Framework to create desktop shortcuts

This article will introduce how to programmatically create a desktop shortcut pointing to a specific EXE file using Windows official API and .NET Framework 3.5.

The following code demonstrates how to create a shortcut and set additional options such as hotkeys and description:

using IWshRuntimeLibrary;

public class ShortcutCreator
{
    public void CreateShortcut()
    {
        object shDesktop = (object)"Desktop";
        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();
    }
}
Copy after login

By referencing the Windows Script Host Object Model, we can access the classes and methods required to create shortcuts. The WshShell class provides access to the "Special Folders" collection from which the paths to desktop folders can be retrieved. The IWshShortcut interface allows us to set the shortcut's properties, such as its description, hotkey, and target path.

This code provides a reliable and flexible way to create desktop shortcuts, making it a valuable tool for automating desktop management tasks.

The above is the detailed content of How Can I Programmatically Create a Desktop Shortcut Using .NET Framework?. 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