【C#】创建快捷方式

创建快捷方式

需要先引入IWshRuntimeLibrary。在添加引用对话框中搜索Windows Script Host Object Model

 创建快捷方式代码如下

public partial class FileHelper
{
    #region 创建快捷方式
    //需要引入IWshRuntimeLibrary,搜索Windows Script Host Object Model
    /// <summary>
    /// 创建快捷方式
    /// </summary>
    /// <param name="directory">快捷方式所处的文件夹</param>
    /// <param name="shortcutName">快捷方式名称</param>
    /// <param name="targetPath">目标路径</param>
    /// <param name="description">描述</param>
    /// <param name="iconLocation">图标路径,格式为"可执行文件或DLL路径, 图标编号",
    /// 例如System.Environment.SystemDirectory + "\" + "shell32.dll, 165"</param>
    /// <remarks></remarks>
    public static void CreateShortcut(string directory, string shortcutName, string targetPath,
        string description = null, string iconLocation = null)
    {
        if (!System.IO.Directory.Exists(directory))
        {
            System.IO.Directory.CreateDirectory(directory);
        }

        if (!System.IO.File.Exists(targetPath) && !System.IO.Directory.Exists(targetPath))
        {
            return;
        }

        string shortcutPath = Path.Combine(directory, string.Format("{0}.lnk", shortcutName));
        WshShell shell = new WshShell();
        IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutPath);//创建快捷方式对象
        shortcut.TargetPath = targetPath;//指定目标路径
        shortcut.WorkingDirectory = Path.GetDirectoryName(targetPath);//设置起始位置
        shortcut.WindowStyle = 1;//设置运行方式,默认为常规窗口
        shortcut.Description = description;//设置备注
        shortcut.IconLocation = string.IsNullOrWhiteSpace(iconLocation) ? targetPath : iconLocation;//设置图标路径
        shortcut.Save();//保存快捷方式
    }

    /// <summary>
    /// 创建桌面快捷方式
    /// </summary>
    /// <param name="shortcutName">快捷方式名称</param>
    /// <param name="targetPath">目标路径</param>
    /// <param name="description">描述</param>
    /// <param name="iconLocation">图标路径,格式为"可执行文件或DLL路径, 图标编号"</param>
    /// <remarks></remarks>
    public static void CreateShortcutOnDesktop(string shortcutName, string targetPath,
        string description = null, string iconLocation = null)
    {
        CreateShortcut(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), shortcutName, targetPath, description, iconLocation);
    }

    #endregion
}

参考:使用C#创建快捷方式

### 回答1: 要在 PowerShell 中创建快捷方式,您需要使用 New-Object cmdlet 和 WScript.Shell COM 对象。 示例代码如下: ``` $WshShell = New-Object -ComObject WScript.Shell $Shortcut = $WshShell.CreateShortcut("C:\Path\To\Shortcut.lnk") $Shortcut.TargetPath = "C:\Path\To\Target.exe" $Shortcut.Save() ``` 在这里,$Shortcut 变量保存着新创建快捷方式的对象。然后,您可以使用 TargetPath 属性指定快捷方式指向的目标文件的路径。最后,使用 Save() 方法将快捷方式保存到指定的路径。 您还可以使用其他属性来设置快捷方式的其他属性,例如 Description、WorkingDirectory 等。有关详细信息,请参阅 MSDN 文档:https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/windows-scripting/8k1kstfh(v=vs.84) 希望这能帮助您。 ### 回答2: 在PowerShell中创建快捷方式的方法是通过使用`New-Object`命令和`WScript.Shell`对象来完成。下面是详细的步骤: 1. 打开PowerShell控制台。 2. 使用以下命令创建一个`WScript.Shell`对象: ```powershell $shell = New-Object -ComObject WScript.Shell ``` 这会实例化一个`WScript.Shell`对象,并将其赋给`$shell`变量。 3. 使用`$shell.CreateShortcut`方法来创建快捷方式。指定快捷方式的目标、快捷方式的名称和快捷方式的保存位置。例如: ```powershell $shortcut = $shell.CreateShortcut("C:\Users\用户名\Desktop\新快捷方式.lnk") ``` 这将在桌面上创建一个名为"新快捷方式"的快捷方式。 4. 设置快捷方式的目标路径和其他属性。例如,如果要指定计算器应用程序作为快捷方式的目标,则可以使用以下命令: ```powershell $shortcut.TargetPath = "C:\Windows\System32\calc.exe" ``` 5. 使用`$shortcut.Save()`方法保存创建快捷方式。 ```powershell $shortcut.Save() ``` 6. 最后,关闭PowerShell控制台。 通过按照上述步骤在PowerShell中创建快捷方式,您可以为特定应用程序或文件创建自定义的桌面或文件夹快捷方式。 ### 回答3: 要使用Powershell创建快捷方式,可以按照以下步骤进行: 1. 打开Powershell控制台:在开始菜单中搜索“powershell”,然后选择“Windows Powershell”。 2. 输入以下命令以创建快捷方式: ``` $WshShell = New-Object -ComObject WScript.Shell $Shortcut = $WshShell.CreateShortcut("C:\路径\快捷方式.lnk") $Shortcut.TargetPath = "目标文件的路径" $Shortcut.IconLocation = "图标文件的路径" $Shortcut.WorkingDirectory = "快捷方式的工作目录" $Shortcut.Save() ``` 确保替换“C:\路径\快捷方式.lnk”为您想要创建快捷方式的路径,将“目标文件的路径”替换为快捷方式指向的文件路径,将“图标文件的路径”替换为快捷方式的图标路径,而“快捷方式的工作目录”是可选的,可以设置为期望的目录。 3. 运行命令后,将在指定路径中创建带有给定名称的快捷方式。您可以在资源管理器中导航到该路径以查看此快捷方式。 使用Powershell可以轻松地创建快捷方式,并且可以通过更改上述命令中的属性来自定义快捷方式的目标、图标和工作目录等属性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GreAmbWang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值