C#创建快捷方式

在c#中创建快捷方式

 1 None.gif using  System;
 2 None.gif using  System.Collections.Generic;
 3 None.gif using  System.Text;
 4 None.gif using  IWshRuntimeLibrary;
 5 None.gif // 在引用里面添加“Windows Script Host Object Model”
 6 None.gif namespace  Shortcuts
 7 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 8InBlock.gif    public class Lnk
 9ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
10InBlock.gif        //"FolderPath"快捷方式存放的位置
11InBlock.gif        //"PathLink"指向连接的文件
12InBlock.gif        //"LnkName"快捷方式的文件
13InBlock.gif        //"LnkNote"快捷方式的备注
14InBlock.gif        //"IconLocationPath"指定快捷方式的图标
15InBlock.gif        public void CreateShortcutLnk(string FolderPath, string PathLink, string LnkName, string LnkNote, string IconLocationPath)
16ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
17InBlock.gif            try
18ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
19InBlock.gif                WshShell shell = new WshShell();
20InBlock.gif                IWshShortcut Shortcut = (IWshShortcut)shell.CreateShortcut(FolderPath + "\\" + LnkName + ".lnk");
21InBlock.gif                Shortcut.TargetPath = PathLink;
22InBlock.gif                Shortcut.WindowStyle = 1;
23InBlock.gif                Shortcut.Description = LnkNote;  
24InBlock.gif                Shortcut.IconLocation = IconLocationPath;
25InBlock.gif                Shortcut.Save();
26ExpandedSubBlockEnd.gif            }

27InBlock.gif            catch
28ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
29InBlock.gif                throw new Exception("出错了");
30ExpandedSubBlockEnd.gif            }

31ExpandedSubBlockEnd.gif        }

32ExpandedSubBlockEnd.gif    }

33ExpandedBlockEnd.gif}

34 None.gif

上面的代码就是在C#中创建快捷方式的核心部分。其中这个类需要在引用中添加“Windows Script Host Object Model”并且USING IWshRuntimeLibrary
下面是一个在VS.NET2005中制作的WINFORM程序。这个程序将演示如何在桌面上创建自己的快捷方式。
 1 None.gif using  System;
 2 None.gif using  System.Collections.Generic;
 3 None.gif using  System.ComponentModel;
 4 None.gif using  System.Data;
 5 None.gif using  System.Drawing;
 6 None.gif using  System.Text;
 7 None.gif using  System.Windows.Forms;
 8 None.gif
 9 None.gif namespace  创建快捷方式
10 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
11InBlock.gif    public partial class Form1 : Form
12ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
13InBlock.gif        public Form1()
14ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
15InBlock.gif            InitializeComponent();
16ExpandedSubBlockEnd.gif        }

17InBlock.gif
18InBlock.gif        private void button1_Click(object sender, EventArgs e)
19ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
20InBlock.gif            Shortcuts.Lnk CreateLnk = new Shortcuts.Lnk();
21InBlock.gif            CreateLnk.CreateShortcutLnk(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory),
22InBlock.gif                Application.ExecutablePath, "我的应用程序""我的应用程序", Application.ExecutablePath);
23InBlock.gif            MessageBox.Show("成功创建快捷方式");
24ExpandedSubBlockEnd.gif        }

25ExpandedSubBlockEnd.gif    }

26ExpandedBlockEnd.gif}

源码下载

转载于:https://www.cnblogs.com/LIfelse/archive/2006/01/11/qwe.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值