代码库の创建快捷方式
- using System.Runtime.InteropServices;//互动服务
- using IWshRuntimeLibrary;
- using System;
-
- namespace myMethod
- {
- public class CreateLink
- {
- /* 1.首先要添加引用.
- * 选择 COM 选项卡并选择 Windows Script Host Object Model
- *
- * 2.引用命名空间
- * using System.Runtime.InteropServices;//互动服务
- * using IWshRuntimeLibrary;
- *
- * 3.创建快捷方式(注释中有详细说明)
- */
-
-
-
- /// <summary>
- /// 创建快捷方式方法的参数为:快捷方式名 + 描述 + 参数+ 自定义图标 +快捷键
- /// </summary>
- /// <param name="strLINKpath">输入要在何处创建快递方式。默认为系统桌面。</param>
- /// <param name="strAPPname">快捷方式的名称。</param>
- /// <param name="intStyle">目标应用程序窗口类型(1.Normal window普通窗口,3.Maximized最大化窗口,7.Minimized最小化)</param>
- /// <param name="strAppDescription">快捷方式的描述 </param>
- /// <param name="strICONpath">自定义快捷方式图标的路径,默认为源文件的图标。 </param>
- /// <param name="strArguments">设置应用程序的启动参数,如"/xx /xx"</param>
- /// <param name="strHotKey">设置快捷键 如:"CTRL+ALT+S"</param>
- public static void myMethod_CreateLINK(string strLINKpath,
- string strAPPname ,
- int intStyle,
- string strAppDescription ,
- string strICONpath,
- string strArguments,
- string strHotKey)
- {
-
- //实例化WshShell对象
- WshShell shell = new WshShell();
-
- //通过该对象的 CreateShortcut 方法来创建 IWshShortcut 接口的实例对象
- IWshShortcut shortcut;
- if ( strLINKpath == "" )
- {
- shortcut = (IWshShortcut)shell.CreateShortcut(
- Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\" + strAPPname + ".lnk");
-
- }
- else
- {
- shortcut = (IWshShortcut)shell.CreateShortcut(strLINKpath + "\\" + strAPPname + ".lnk");
-
- }
-
- //设置快捷方式的目标所在的位置(源程序完整路径)
- shortcut.TargetPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
-
- //应用程序的工作目录
- //当用户没有指定一个具体的目录时,快捷方式的目标应用程序将使用该属性所指定的目录来装载或保存文件。
- shortcut.WorkingDirectory = System.Environment.CurrentDirectory;
-
- //目标应用程序窗口类型(1.Normal window普通窗口,3.Maximized最大化窗口,7.Minimized最小化)
-
- switch ( intStyle )
- {
- case 1:
- shortcut.WindowStyle = 1;
- break;
- case 3:
- shortcut.WindowStyle = 3;
- break;
- case 7:
- shortcut.WindowStyle = 7;
- break;
- default :
- shortcut.WindowStyle = 1;
- break;
-
- }
- //快捷方式的描述
- shortcut.Description = strAppDescription;
-
- //可以自定义快捷方式图标.(如果不设置,则将默认源文件图标.)
- //shortcut.IconLocation = System.Environment.SystemDirectory + "\\" + "shell32.dll, 165";
- if(strICONpath != "")
- shortcut.IconLocation = strICONpath;
-
- //设置应用程序的启动参数(如果应用程序支持的话)
- if (strAppDescription != "")
- shortcut.Arguments = strAppDescription;
-
- //设置快捷键(如果有必要的话.) 如:"CTRL+ALT+D";
- if (strHotKey !="")
- shortcut.Hotkey = strHotKey;
-
- //保存快捷方式
- shortcut.Save();
-
- }
-
- }
-
-
- }