C#-创建应用程序快捷方式

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.Runtime.InteropServices;
using IWshRuntimeLibrary;
using System.IO;

namespace CreateProShortKey
{
    public partial class Frm_Main : Form
    {
        public Frm_Main()
        {
            InitializeComponent();
        }
        string exePath = "";
        private void button1_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                exePath = openFileDialog1.FileName;
                textBox1.Text = exePath;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (exePath.Length == 0)//判断是否选择了应用程序
            {
                MessageBox.Show("请选择应用程序");//如果没有选择则弹出提示信息
            }
            else//如果选择了应用程序
            {
                WshShell sl = new WshShell();//创建WshShell对象
                //设置桌面快捷方式的路径
                string dtpath = System.Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "\\wxk.lnk";
                //设置开始菜单里的快捷方式路径
                string dtpath1 = System.Environment.GetFolderPath(Environment.SpecialFolder.StartMenu) + "\\程序\\小科\\wxk.lnk";
                string dd = Path.GetDirectoryName(dtpath1);    //判断开始菜单中指定的文件夹是否存在
                if (!Directory.Exists(dd))
                {
                    Directory.CreateDirectory(dd);//如果不存在则创建该文件夹
                }
                IWshShortcut sc = (IWshShortcut)sl.CreateShortcut(dtpath1);//创建IwshShortcut对象
                sc.TargetPath = exePath;//设置应用程序路径
                sc.Description = "创建应用程序的快捷方式";//添加描述信息
                sc.Save();//保存快捷方式
                IWshShortcut sc1 = (IWshShortcut)sl.CreateShortcut(dtpath);//创建IwshShortcut对象
                sc1.TargetPath = exePath;//设置应用程序路径
                sc1.Description = "创建应用程序的快捷方式";//添加描述信息
                sc1.Save();//保存快捷方式
                MessageBox.Show("创建快捷方式成功");//弹出提示信息
            }
        }
    }
}
 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值