湖上湖
干净。(.net 4.0)Type t = Type.GetTypeFromCLSID(new Guid("72C24DD5-D70A-438B-8A42-98424B88AFB8")); //Windows Script Host Shell Objectdynamic shell = Activator.CreateInstance(t);try{
var lnk = shell.CreateShortcut("sc.lnk");
try{
lnk.TargetPath = @"C:\something";
lnk.IconLocation = "shell32.dll, 1";
lnk.Save();
}finally{
Marshal.FinalReleaseComObject(lnk);
}}finally{
Marshal.FinalReleaseComObject(shell);}就是这样,不需要额外的代码。Create捷径甚至可以从文件中加载快捷方式,因此属性如下TargetPath返回现有信息。快捷对象属性.对于.NET不支持动态类型的版本,也可能采用这种方式。(.净3.5)Type t = Type.GetTypeFromCLSID(new Guid("72C24DD5-D70A-438B-8A42-98424B88AFB8")); //Windows Script Host Shell Objectobject shell = Activator.CreateInstance(t);try{
object lnk = t.InvokeMember("CreateShortcut", BindingFlags.InvokeMethod, null, shell, new object[]{"sc.lnk"});
try{
t.InvokeMember("TargetPath", BindingFlags.SetProperty, null, lnk, new object[]{@"C:\whatever"});
t.InvokeMember("IconLocation", BindingFlags.SetProperty, null, lnk, new object[]{"shell32.dll, 5"});
t.InvokeMember("Save", BindingFlags.InvokeMethod, null, lnk, null);
}finally{
Marshal.FinalReleaseComObject(lnk);
}}finally{
Marshal.FinalReleaseComObject(shell);}