到特定目录下,那么在.net上我并未发现有什么托管类可以操作快捷方式,那么我们
using system; using system.io; using system.runtime.interopservices; class program { static void main(string[] args) { createshortcut( // 创建快捷方式 @c:\users\windo\desktop\ican.lnk, @%homedrive%/program files\internet explorer\iexplore.exe, appdomain.currentdomain.basedirectory, @%homedrive%/program files\internet explorer\iexplore.exe, 0, ctrl+alt+z ); } public static readonly guid clsid_wshshell = new guid(72c24dd5-d70a-438b-8a42-98424b88afb8); public static string getshortcuttarget(string lnk) // 取快捷方式目标 { if (lnk != null && file.exists(lnk)) { dynamic objwshshell = null, objshortcut = null; try { objwshshell = activator.createinstance(type.gettypefromclsid(clsid_wshshell)); objshortcut = objwshshell.createshortcut(lnk); return objshortcut.targetpath; } finally { marshal.releasecomobject(objshortcut); marshal.releasecomobject(objwshshell); } } return string.empty; } public static bool createshortcut(string lnkfilename, string targetpath, string arguments, string remark, string workingdirectory, string iconlocation, string hotkey ) { if (lnkfilename != null && lnkfilename.length > 0) { dynamic objwshshell = null, objshortcut = null; try { objwshshell = activator.createinstance(type.gettypefromclsid(clsid_wshshell)); objshortcut = objwshshell.createshortcut(lnkfilename); objshortcut.windowstyle = 1; objshortcut.hotkey = hotkey; // 热键 objshortcut.targetpath = targetpath; // 目标文件 objshortcut.arguments = arguments; // 参数 objshortcut.description = remark; // 备注 objshortcut.workingdirectory = workingdirectory; // 起始位置 objshortcut.iconlocation = iconlocation; // 图标位置 objshortcut.save(); return true; } finally { marshal.releasecomobject(objshortcut); marshal.releasecomobject(objwshshell); } } return false; } }
