使用C#创建,查看,修改,删除快捷方式

使用C#创建,查看,修改,删除快捷方式

  1. 引用COM组件
    1)右键“引用”,“添加引用”。
    右键“引用”,“添加引用”
    2) 选择“COM组件”,找到“Windows Script Host Object Model”,然后确定。
    选择“COM组件”
  2. 创建快捷方式
        /// <summary>
        /// 为某个文件创建快捷方式(例如为xxx.exe创建快捷方式)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCreate_Click(object sender, EventArgs e)
        {


            //创建快捷方式前,先获取要创建快捷方式的文件信息(例如xxx.exe的文件信息)
            string workingDirectory = @"C:\Users\AY_Format\Desktop\超强超快隐藏窗口软件\";           //获取该文件(如xxx.exe)的所处文件夹路径
            string targetPath = @"C:\Users\AY_Format\Desktop\超强超快隐藏窗口软件\QuickHider.exe";   //获取该文件(如xxx.exe)的所处路径


            //创建快捷方式的步骤
            string directory = @"C:\Users\AY_Format\Desktop\";    //创建的快捷方式后,该快捷方式所处的文件夹的路径(根据情况自定路径)
            string shortcutName = "QuickHider快捷方式.lnk";       //要创建的快捷方式名称(.lnk为快捷方式后缀)
            WshShell shell = new WshShell();
            IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(directory + shortcutName);    //创建快捷方式对象
            shortcut.TargetPath = targetPath;                 //指定目标路径                        (右键快捷方式的目标(T))
            shortcut.WorkingDirectory = workingDirectory;     //设置起始位置                        (右键快捷方式的起始位置(S))
            shortcut.WindowStyle = 1;                         //设置运行方式,默认为常规窗口        (右键快捷方式的运行方式(R))
            shortcut.Description = "这是用代码创建的快捷方式";//设置备注,鼠标放在图标上提示改文字  (右键快捷方式的备注(O))
            //shortcut.IconLocation = "";                     //设置图标路径,这里不设置            (快捷方式的图标)
            shortcut.Save();                                  //保存快捷方式


        }
  1. 查看快捷方式
        /// <summary>
        /// 读取快捷方式信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRead_Click(object sender, EventArgs e)
        {
            string initialSource = @"C:\Users\AY_Format\Desktop\QuickHider快捷方式.lnk"; //需要读取的快捷方式路径
            WshShell shell = new WshShell();
            IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(initialSource);    //获取快捷方式对象
            string targetPath = shortcut.TargetPath;                 //读取右键快捷方式的目标信息
            string workingDirectory = shortcut.WorkingDirectory;     //读取右键快捷方式的起始位置信息
            int windowStyle = shortcut.WindowStyle;                  //读取右键快捷方式的运行方式信息
            string description = shortcut.Description;               //读取右键快捷方式的备注信息
            string iconLocation = shortcut.IconLocation;             //读取快捷方式的图标信息
            lblInfo.Text = "目标信息:" + targetPath + "\n起始位置信息:" + workingDirectory + "\n运行方式信息:" + windowStyle + "\n备注信息:" + description + "\n图标信息:" + iconLocation;
        }
  1. 修改快捷方式
        /// <summary>
        /// 修改快捷方式属性
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            string initialSource = @"C:\Users\AY_Format\Desktop\QuickHider快捷方式.lnk"; //获取要更改的快捷方式路径
            WshShell shell = new WshShell();
            IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(initialSource);    //获取快捷方式对象
            shortcut.Description = "这是修改后的备注信息!";      //修改备注,鼠标放在图标上提示改文字  (右键快捷方式的备注(O))
            //其他属性也是如此
            shortcut.Save();
        }
  1. 删除快捷方式
        /// <summary>
        /// 删除快捷方式
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (System.IO.File.Exists(@"C:\Users\AY_Format\Desktop\QuickHider快捷方式.lnk"))
            {
                System.IO.File.Delete(@"C:\Users\AY_Format\Desktop\QuickHider快捷方式.lnk");
                MessageBox.Show("删除成功!");
            }
        }

完整代码

using IWshRuntimeLibrary;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;


namespace Shortcut
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 为某个文件创建快捷方式(例如为xxx.exe创建快捷方式)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCreate_Click(object sender, EventArgs e)
        {


            //创建快捷方式前,先获取要创建快捷方式的文件信息(例如xxx.exe的文件信息)
            string workingDirectory = @"C:\Users\AY_Format\Desktop\超强超快隐藏窗口软件\";           //获取该文件(如xxx.exe)的所处文件夹路径
            string targetPath = @"C:\Users\AY_Format\Desktop\超强超快隐藏窗口软件\QuickHider.exe";   //获取该文件(如xxx.exe)的所处路径


            //创建快捷方式的步骤
            string directory = @"C:\Users\AY_Format\Desktop\";    //创建的快捷方式后,该快捷方式所处的文件夹的路径(根据情况自定路径)
            string shortcutName = "QuickHider快捷方式.lnk";       //要创建的快捷方式名称(.lnk为快捷方式后缀)
            WshShell shell = new WshShell();
            IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(directory + shortcutName);    //创建快捷方式对象
            shortcut.TargetPath = targetPath;                 //指定目标路径                        (右键快捷方式的目标(T))
            shortcut.WorkingDirectory = workingDirectory;     //设置起始位置                        (右键快捷方式的起始位置(S))
            shortcut.WindowStyle = 1;                         //设置运行方式,默认为常规窗口        (右键快捷方式的运行方式(R))
            shortcut.Description = "这是用代码创建的快捷方式";//设置备注,鼠标放在图标上提示改文字  (右键快捷方式的备注(O))
            //shortcut.IconLocation = "";                     //设置图标路径,这里不设置            (快捷方式的图标)
            shortcut.Save();                                  //保存快捷方式


        }

        /// <summary>
        /// 读取快捷方式信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRead_Click(object sender, EventArgs e)
        {
            string initialSource = @"C:\Users\AY_Format\Desktop\QuickHider快捷方式.lnk"; //需要读取的快捷方式路径
            WshShell shell = new WshShell();
            IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(initialSource);    //获取快捷方式对象
            string targetPath = shortcut.TargetPath;                 //读取右键快捷方式的目标信息
            string workingDirectory = shortcut.WorkingDirectory;     //读取右键快捷方式的起始位置信息
            int windowStyle = shortcut.WindowStyle;                  //读取右键快捷方式的运行方式信息
            string description = shortcut.Description;               //读取右键快捷方式的备注信息
            string iconLocation = shortcut.IconLocation;             //读取快捷方式的图标信息
            lblInfo.Text = "目标信息:" + targetPath + "\n起始位置信息:" + workingDirectory + "\n运行方式信息:" + windowStyle + "\n备注信息:" + description + "\n图标信息:" + iconLocation;
        }

        /// <summary>
        /// 修改快捷方式属性
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            string initialSource = @"C:\Users\AY_Format\Desktop\QuickHider快捷方式.lnk"; //获取要更改的快捷方式路径
            WshShell shell = new WshShell();
            IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(initialSource);    //获取快捷方式对象
            shortcut.Description = "这是修改后的备注信息!";      //修改备注,鼠标放在图标上提示改文字  (右键快捷方式的备注(O))
            //其他属性也是如此
            shortcut.Save();
        }

        /// <summary>
        /// 删除快捷方式
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (System.IO.File.Exists(@"C:\Users\AY_Format\Desktop\QuickHider快捷方式.lnk"))
            {
                System.IO.File.Delete(@"C:\Users\AY_Format\Desktop\QuickHider快捷方式.lnk");
                MessageBox.Show("删除成功!");
            }
        }

    }
}

源码下载(窗体+代码):使用C#创建,查看,修改,删除快捷方式
窗体

  • 1
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值