You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何自定义基于C#和.NET 4.7的开始菜单快捷方式属性?

实现快捷方式的属性自定义

1. 设置启动方式(窗口状态)

IWshShortcut自带WindowStyle属性,可直接设置对应窗口状态:

  • 1:正常窗口(默认)
  • 3:最大化窗口
  • 7:最小化窗口

在代码中添加对应设置:

exe1.WindowStyle = 3; // 示例:设置为最大化启动

2. 设置“打开文件位置”(工作目录)

通过WorkingDirectory属性指定应用程序的工作目录,对应右键菜单的“打开文件位置”:

// 设置为应用程序所在目录
exe1.WorkingDirectory = Application.StartupPath;

3. 启用“以管理员身份运行”

IWshRuntimeLibrary未直接暴露该属性,需手动修改快捷方式文件字节——“以管理员运行”的标志存储在lnk文件的第21字节(0索引),将其改为0x20即可。

添加辅助方法完成修改:

private void SetRunAsAdministrator(string shortcutPath)
{
    byte[] shortcutBytes = File.ReadAllBytes(shortcutPath);
    shortcutBytes[21] = 0x20;
    File.WriteAllBytes(shortcutPath, shortcutBytes);
}

在保存快捷方式后调用该方法即可生效。


完整修改后的代码

需添加using System.IO;引用,最终按钮点击方法如下:

private void button1_Click(object sender, EventArgs e)
{
    string shortcutPath = @"C:\Users\DIVYASRI\Start\exe1.lnk";
    WshShell shell = new WshShell();
    IWshShortcut exe1 = (IWshShortcut)shell.CreateShortcut(shortcutPath);
    
    exe1.TargetPath = Application.ExecutablePath;
    exe1.Description = "Launch the App";
    // 设置启动方式为最大化
    exe1.WindowStyle = 3;
    // 设置工作目录
    exe1.WorkingDirectory = Application.StartupPath;
    
    exe1.Save();
    // 启用管理员运行
    SetRunAsAdministrator(shortcutPath);
}

private void SetRunAsAdministrator(string shortcutPath)
{
    byte[] shortcutBytes = File.ReadAllBytes(shortcutPath);
    shortcutBytes[21] = 0x20;
    File.WriteAllBytes(shortcutPath, shortcutBytes);
}

内容的提问来源于stack exchange,提问作者Divya Sri Thokala

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 08:28:15