C# 创建快捷方式 / 取快捷方式目标

快捷方式在win32上应用的非常之多,比如某个软件安装完毕后会创建一些快捷方式

到特定目录下,那么在.NET上我并未发现有什么托管类可以操作快捷方式,那么我们

using System;  using System.IO;  using System.Runtime.InteropServices;  class Program  {      static void Main(string[] args)      {          CreateShortCut( // 创建快捷方式                  @"C:UserswindoDesktopican.lnk",                  @"%HOMEDRIVE%/Program FilesInternet ExplorerIEXPLORE.EXE",                                               AppDomain.CurrentDomain.BaseDirectory,                 @"%HOMEDRIVE%/Program FilesInternet ExplorerIEXPLORE.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;      }  }

登录后复制

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2445933.html

(0)
上一篇 2025年3月3日 16:18:49
下一篇 2025年2月26日 03:36:05

AD推荐 黄金广告位招租... 更多推荐

相关推荐

发表回复

登录后才能评论