Favicon

Create shortcut

Peponi11/19/20241m

C#
COMIWshRuntimeLibraryLink.lnk

1. Introduction

아래는 코드를 이용해 바로가기를 생성하는 방법이다. COM 라이브러리 중 Windows Script Host Object Model을 추가한 후, IWshRuntimeLibraryusing 선언하여 사용한다.

2. Code

using System;
using System.IO;
using System.Windows.Forms;
using IWshRuntimeLibrary;
 
namespace CreateShortcut
{
    public static class Shortcut
    {
        private static string _appStartMenuPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Programs), "MyProject");
 
        public static void AddShortcut()
        {
            if (!Directory.Exists(_appStartMenuPath))
                Directory.CreateDirectory(_appStartMenuPath);
 
            string exeLocation = Path.Combine(_appStartMenuPath, "MyProject.exe" + ".lnk");
            WshShell shell = new WshShell();
            IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(exeLocation);
 
            shortcut.Description = "MyProject's description";
            shortcut.IconLocation = Application.StartupPath + "projectIcon.ico";
            shortcut.TargetPath = Application.ExecutablePath;
            shortcut.WorkingDirectory = Application.StartupPath;
            shortcut.Save();
        }
    }
}