分享

C#创建快捷方式

 昵称PAi0R 2010-10-31

现将C#创建快捷方式总结如下:

1、快捷方式包含如下数据:

  ·快捷方式的名字

  ·快捷方式所指向的目标所在的位置

  ·快捷方式所指向的目标的工作目录

  ·激活该快捷方式的热键

  ·快捷方式所指向的目标运行时的窗口风格(普通、最大化和最小化)

  ·该快捷方式的描述性文字

  ·快捷方式的图标所在的位置

2、在工程中选择 COM 选项卡并选择 Windows Script Host Object Model。

如图:

C#如何创建快捷方式之一
3、代码如下:
using System;
using System.Collections.Generic;
using System.Collections;
using System.IO;
using System.Reflection;
using IWshRuntimeLibrary;

namespace Test
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            //创建快捷方式
            //建立对象
            WshShell shell = new WshShell();
            //生成快捷方式文件,指定路径及文件名
            IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(
    Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) +
    "\\" + "sunlunjun.lnk");
            //快捷方式指向的目标
            shortcut.TargetPath = Path.Combine(System.Environment.CurrentDirectory, "myService.exe");
            //起始目录
            shortcut.WorkingDirectory = System.Environment.CurrentDirectory;
            //窗口类型
            shortcut.WindowStyle = 1;
            //描述
            shortcut.Description = "my Application";
            //图标
            shortcut.IconLocation = System.Environment.SystemDirectory + "\\" + "shell32.dll, 165";
            //保存,注意一定要保存,否则无效
            shortcut.Save();
        }
    }
}

4、这样,我们创建一个 WshShell 的实例对象,接着通过该对象的CreateShortcut 方法来创建 IWshShortcut 接口的实例对象,传递给CreateShortcut 方法的参数是将要创建的快捷方式的完整路径(包括该快捷方式的名字)。接下来,我们就要设置 IWshShortcut 实例对象的相关属性值了。

5、生成快捷方式
    CreateShortcut 仅仅创建一个 IWshShortcut 的实例对象,它不会为你生成任何快捷方式,当一切就绪后,你必须调用 IWshShortcut.Save 方法才能生成快捷方式文件。


注:如果是对网络资源的链接,只需将TargetPath 设成相应的地址即可。

(比如:TargetPath = "http://blog.sina.com.cn/lunjunsun"; )

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多