首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

C# 建立捷徑方法介紹:WSH COM 物件實現

     編輯:關於C語言

捷徑本質

.lnk 檔案,包含:名稱、目標路徑、工作目錄、熱鍵、視窗樣式、描述、圖示路徑。

使用 WSH (Windows Script Host) 建立捷徑

1. 加入 COM 參考

專案右鍵 → 加入參考 → COM 標籤 → Windows Script Host Object Model

2. 完整建立代碼


using System;
using IWshRuntimeLibrary;

class Program
{
    static void Main(string[] args)
    {
        WshShell shell = new WshShell();
        IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(
            Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) 
            + "\\" + "Allen's Application.lnk");
        
        shortcut.TargetPath = Assembly.GetExecutingAssembly().Location;
        shortcut.WorkingDirectory = Environment.CurrentDirectory;
        shortcut.WindowStyle = 1; // 1=正常, 3=最大化, 7=最小化
        shortcut.Description = "Launch Allen's Application";
        shortcut.IconLocation = Environment.SystemDirectory + "\\shell32.dll, 165";
        shortcut.Save();
    }
}

關鍵屬性說明

優勢

  • 純 .NET 程式碼,無需 P/Invoke
  • WSH 為系統內建,相容性極佳
  • 支援所有捷徑屬性設定
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved
屬性用途
TargetPath目標執行檔完整路徑
WorkingDirectory啟動工作目錄
WindowStyle1=正常, 3=最大化, 7=最小化
Description滑鼠懸停提示文字
IconLocation圖示檔路徑 + 索引 (shell32.dll 內建圖示)
Hotkey熱鍵 (如 "Ctrl+Alt+A")
Arguments命令列參數