创建快捷方式

这是微软用来创建快捷方式的类库,当在项目中添加Com引用"Windows Script Host Object Model 
  "时,此类库就会出现在项目中。下面以C#项目为例介绍使用方法:

  1.首先要添加引用. 
  添加引用的方法非常简单,右击你的项目并选择添加引用, 
  选择 COM 选项卡并选择 Windows Script Host Object Model

  2.引用命名空间 
  using System.Runtime.InteropServices;//互动服务 
  using IWshRuntimeLibrary;

  3.创建快捷方式(注释中有详细说明) 
  //实例化WshShell对象 
  WshShell shell = new WshShell(); 
  //通过该对象的 CreateShortcut 方法来创建 IWshShortcut 接口的实例对象 
  IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut( 
  Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "//ShortCut.lnk"); 
  //设置快捷方式的目标所在的位置(源程序完整路径) 
  shortcut.TargetPath = System.Reflection.Assembly.GetExecutingAssembly().Location; 
  //应用程序的工作目录 
  //当用户没有指定一个具体的目录时,快捷方式的目标应用程序将使用该属性所指定的目录来装载或保存文件。 
  shortcut.WorkingDirectory = System.Environment.CurrentDirectory; 
  //目标应用程序窗口类型(1.Normal window普通窗口,3.Maximized最大化窗口,7.Minimized最小化) 
  shortcut.WindowStyle = 1; 
  //快捷方式的描述 
  shortcut.Description = "ChinaDforce YanMang"; 
  //可以自定义快捷方式图标.(如果不设置,则将默认源文件图标.) 
  //shortcut.IconLocation = System.Environment.SystemDirectory + "\\" + "shell32.dll, 165"; 
  //设置应用程序的启动参数(如果应用程序支持的话) 
  //shortcut.Arguments = "/myword /d4s"; 
  //设置快捷键(如果有必要的话.) 
  //shortcut.Hotkey = "CTRL+ALT+D"; 
  //保存快捷方式 
  shortcut.Save(); 
  缺点: 
  用这种方法写的程序,必须有Interop.IWshRuntimeLibrary.dll跟着, 
  才能正确执行.对于创建"单文件程序"的人来讲,麻烦了吧.
posted @ 2014-05-29 10:15  ChineseMoonGod  阅读(262)  评论(0编辑  收藏  举报