From 438c09e9584d6ed71dc77cf06823cb6a253ee24b Mon Sep 17 00:00:00 2001 From: Elivo Date: Mon, 23 Feb 2026 13:55:04 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=88=9B=E5=BB=BA=20.url=20?= =?UTF-8?q?=E7=9A=84=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Apewer.Windows/WindowsUtility.cs | 38 ++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/Apewer.Windows/WindowsUtility.cs b/Apewer.Windows/WindowsUtility.cs index e0410b2..88d1885 100644 --- a/Apewer.Windows/WindowsUtility.cs +++ b/Apewer.Windows/WindowsUtility.cs @@ -1039,17 +1039,21 @@ namespace Apewer #if NETFX - /// 创建快捷方式。 - /// 快捷方式路径。 + /// 创建快捷方式,生成 .lnk 文件。 + /// 快捷方式路径,以 .lnk 结尾。 /// 快捷方式图标。可使用 c:\source.exe,0 格式。 /// 快捷方式说明。 /// 源路径。 /// 源参数。 /// 工作目录。 + /// public static void CreateShortcut(string linkPath, string sourcePath, string sourceArgs = null, string linkIcon = null, string linkDescription = null, string directory = null) { // var desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); // var shortcut = (IWshShortcut)new WshShellClass().CreateShortcut(linkPath); + if (linkPath.IsEmpty()) throw new ArgumentNullException(nameof(linkPath)); + if (sourcePath.IsEmpty()) throw new ArgumentNullException(nameof(sourcePath)); + var wshShellClass = new IWshRuntimeLibrary.WshShellClass(); var wshObject = wshShellClass.CreateShortcut(linkPath); var wshShortcut = (IWshRuntimeLibrary.IWshShortcut)wshObject; @@ -1067,6 +1071,36 @@ namespace Apewer #endif + /// 创建 URL 快捷方式,生成 .url 文件。 + /// 快捷方式路径,以 .url 结尾。 + /// URL。 + /// 快捷方式图标。 + /// 快捷方式图标索引。 + /// + public static void CreateUrlShortCut(string linkPath, string url, string iconFilePath = null, int iconIndex = 0) + { + if (linkPath.IsEmpty()) throw new ArgumentNullException(nameof(linkPath)); + if (url.IsEmpty()) throw new ArgumentNullException(nameof(url)); + + var lines = new List(); + lines.Add("[{000214A0-0000-0000-C000-000000000046}]"); + lines.Add("Prop3=19,11"); + lines.Add(""); + lines.Add("[InternetShortcut]"); + lines.Add("IDList="); + lines.Add($"URL={url}"); + if (StorageUtility.FileExists(iconFilePath)) + { + lines.Add($"IconIndex={iconIndex}"); + lines.Add("HotKey=0"); + lines.Add($"IconFile={iconFilePath}"); + } + + var text = lines.Join("\r\n") + "\r\n"; + var bytes = text.Bytes(Encoding.Default); + StorageUtility.WriteFile(linkPath, bytes); + } + #endregion }