Browse Source

增加创建 .url 的方法

master
Elivo 2 months ago
parent
commit
438c09e958
  1. 38
      Apewer.Windows/WindowsUtility.cs

38
Apewer.Windows/WindowsUtility.cs

@ -1039,17 +1039,21 @@ namespace Apewer
#if NETFX
/// <summary>创建快捷方式。</summary>
/// <param name="linkPath">快捷方式路径。</param>
/// <summary>创建快捷方式,生成 .lnk 文件。</summary>
/// <param name="linkPath">快捷方式路径,以 .lnk 结尾。</param>
/// <param name="linkIcon">快捷方式图标。可使用 c:\source.exe,0 格式。</param>
/// <param name="linkDescription">快捷方式说明。</param>
/// <param name="sourcePath">源路径。</param>
/// <param name="sourceArgs">源参数。</param>
/// <param name="directory">工作目录。</param>
/// <exception cref="ArgumentNullException" />
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
/// <summary>创建 URL 快捷方式,生成 .url 文件。</summary>
/// <param name="linkPath">快捷方式路径,以 .url 结尾。</param>
/// <param name="url">URL。</param>
/// <param name="iconFilePath">快捷方式图标。</param>
/// <param name="iconIndex">快捷方式图标索引。</param>
/// <exception cref="ArgumentNullException" />
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<string>();
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
}

Loading…
Cancel
Save