/
Shortcut.DesktopIcon.cs
54 lines (47 loc) · 2.54 KB
/
Shortcut.DesktopIcon.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Copyright Bastian Eicher et al.
// Licensed under the GNU Lesser Public License
using NanoByte.Common.Native;
using ZeroInstall.DesktopIntegration.AccessPoints;
namespace ZeroInstall.DesktopIntegration.Windows;
public static partial class Shortcut
{
/// <summary>
/// Creates a new Windows shortcut on the desktop.
/// </summary>
/// <param name="desktopIcon">Information about the shortcut to be created.</param>
/// <param name="target">The target the shortcut shall point to.</param>
/// <param name="iconStore">Stores icon files downloaded from the web as local files.</param>
/// <param name="machineWide">Create the shortcut machine-wide instead of just for the current user.</param>
public static void Create(DesktopIcon desktopIcon, FeedTarget target, IIconStore iconStore, bool machineWide)
{
#region Sanity checks
if (desktopIcon == null) throw new ArgumentNullException(nameof(desktopIcon));
if (iconStore == null) throw new ArgumentNullException(nameof(iconStore));
#endregion
string filePath = GetDesktopPath(desktopIcon.Name, machineWide);
Create(filePath, target, desktopIcon.Command, iconStore);
}
/// <summary>
/// Removes a Windows shortcut from the desktop.
/// </summary>
/// <param name="desktopIcon">Information about the shortcut to be removed.</param>
/// <param name="machineWide">The shortcut was created machine-wide instead of just for the current user.</param>
public static void Remove(DesktopIcon desktopIcon, bool machineWide)
{
#region Sanity checks
if (desktopIcon == null) throw new ArgumentNullException(nameof(desktopIcon));
#endregion
string filePath = GetDesktopPath(desktopIcon.Name, machineWide);
if (File.Exists(filePath)) File.Delete(filePath);
}
/// <summary>
/// Builds a path for a shortcut on the desktop.
/// </summary>
/// <param name="name">The name of the shortcut (without the .lnk ending).</param>
/// <param name="machineWide"><c>true</c> to use the machine-wide desktop; <c>false</c> for the per-user variant.</param>
/// <exception cref="IOException"><paramref name="name"/> contains invalid characters.</exception>
private static string GetDesktopPath(string? name, bool machineWide)
=> Path.Combine(
WindowsUtils.GetFolderPath(machineWide ? Environment.SpecialFolder.CommonDesktopDirectory : Environment.SpecialFolder.DesktopDirectory),
$"{name}.lnk");
}