C# 创建快捷方式 / 取快捷方式目标

快捷方式在Win32上应用的非常之多,比如某个软件安装完毕后会创建一些快捷方式

到特定目录下,那么在.NET上我并未发现有什么托管类可以操作快捷方式,那么我们

又必须要使用它 为此我预先写了一份快捷方式应用的代码,希望对大家有益健康咯。

using System;
using System.IO;
using System.Runtime.InteropServices;
class Program
{
    static void Main(string[] args)
    {
        CreateShortCut( // 创建快捷方式
                @"C:\Users\windo\Desktop\ican.lnk",
                @"%HOMEDRIVE%/Program Files\Internet Explorer\IEXPLORE.EXE",
                @"http://blog.csdn.net/u012395622",
                @"远去的山河 沉寂  恋过的风景 如昔",
               AppDomain.CurrentDomain.BaseDirectory,
               @"%HOMEDRIVE%/Program Files\Internet Explorer\IEXPLORE.EXE, 0",
               "CTRL+ALT+Z"
            );

    }

    public static readonly Guid CLSID_WshShell = new Guid("72C24DD5-D70A-438B-8A42-98424B88AFB8");
    public static string GetShortCutTarget(string lnk) // 取快捷方式目标
    {
        if (lnk != null && File.Exists(lnk))
        {
            dynamic objWshShell = null, objShortcut = null;
            try
            {
                objWshShell = Activator.CreateInstance(Type.GetTypeFromCLSID(CLSID_WshShell));
                objShortcut = objWshShell.CreateShortcut(lnk);
                return objShortcut.TargetPath;
            }
            finally
            {
                Marshal.ReleaseComObject(objShortcut);
                Marshal.ReleaseComObject(objWshShell);
            }
        }
        return string.Empty;
    }

    public static bool CreateShortCut(string lnkFileName,
            string targetPath,
            string arguments,
            string remark,
            string workingDirectory,
            string iconLocation,
            string hotKey
        )
    {
        if (lnkFileName != null && lnkFileName.Length > 0)
        {
            dynamic objWshShell = null, objShortcut = null;
            try
            {
                objWshShell = Activator.CreateInstance(Type.GetTypeFromCLSID(CLSID_WshShell));
                objShortcut = objWshShell.CreateShortcut(lnkFileName);

                objShortcut.WindowStyle = 1;

                objShortcut.Hotkey = hotKey; // 热键
                objShortcut.TargetPath = targetPath; // 目标文件
                objShortcut.Arguments = arguments; // 参数
                objShortcut.Description = remark; // 备注
                objShortcut.WorkingDirectory = workingDirectory; // 起始位置
                objShortcut.IconLocation = iconLocation; // 图标位置

                objShortcut.Save();

                return true;
            }
            finally
            {
                Marshal.ReleaseComObject(objShortcut);
                Marshal.ReleaseComObject(objWshShell);
            }
        }
        return false;
    }
}


  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值