Skip to content

Commit 430e8aa

Browse files
committed
Private the ctors for ShellLinkFile so only our factory methods can be used.
1 parent f79f0fc commit 430e8aa

1 file changed

Lines changed: 46 additions & 39 deletions

File tree

src/PSADT/PSADT/ShortcutManagement/ShellLinkFile.cs

Lines changed: 46 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -26,76 +26,83 @@ namespace PSADT.ShortcutManagement
2626
internal sealed class ShellLinkFile : IDisposable
2727
{
2828
/// <summary>
29-
/// Initializes a new instance of the <see cref="ShellLinkFile"/> class.
3029
/// Creates a new, empty shell link.
3130
/// </summary>
32-
public ShellLinkFile()
33-
{
34-
_shellLink = (IShellLinkW)new ShellLink();
35-
}
36-
37-
/// <summary>
38-
/// Initializes a new instance of the <see cref="ShellLinkFile"/> class and loads an existing shortcut file.
39-
/// </summary>
40-
/// <param name="filePath">The path to the shortcut file to load.</param>
41-
/// <exception cref="ArgumentNullException">Thrown when <paramref name="filePath"/> is null.</exception>
42-
/// <exception cref="ArgumentException">Thrown when <paramref name="filePath"/> is empty or whitespace.</exception>
43-
/// <exception cref="FileNotFoundException">Thrown when the specified file does not exist.</exception>
44-
public ShellLinkFile(string filePath) : this()
31+
/// <returns>A new <see cref="ShellLinkFile"/> instance.</returns>
32+
public static ShellLinkFile New()
4533
{
46-
Load(filePath);
34+
return new();
4735
}
4836

4937
/// <summary>
50-
/// Initializes a new instance of the <see cref="ShellLinkFile"/> class with the specified target path.
38+
/// Creates a new shell link with the specified target path.
5139
/// </summary>
5240
/// <param name="targetPath">The target path for the shortcut.</param>
53-
/// <param name="createNew">Must be <see langword="true"/> to use this constructor. This parameter disambiguates from the file loading constructor.</param>
54-
/// <exception cref="ArgumentException">Thrown when <paramref name="createNew"/> is false.</exception>
55-
public ShellLinkFile(string targetPath, bool createNew) : this()
41+
/// <returns>A new <see cref="ShellLinkFile"/> instance with the target path set.</returns>
42+
public static ShellLinkFile New(string targetPath)
5643
{
57-
if (!createNew)
58-
{
59-
throw new ArgumentException("Use the single-parameter constructor to load an existing file.", nameof(createNew));
60-
}
61-
TargetPath = targetPath;
44+
return new() { TargetPath = targetPath };
6245
}
6346

6447
/// <summary>
65-
/// Finalizes an instance of the <see cref="ShellLinkFile"/> class.
66-
/// </summary>
67-
~ShellLinkFile()
68-
{
69-
Dispose(false);
70-
}
71-
72-
/// <summary>
73-
/// Loads a shortcut file.
48+
/// Loads an existing shortcut file in read-only mode.
7449
/// </summary>
7550
/// <param name="filePath">The path to the shortcut file to load.</param>
51+
/// <returns>A new <see cref="ShellLinkFile"/> instance loaded from the specified file.</returns>
7652
/// <exception cref="ArgumentNullException">Thrown when <paramref name="filePath"/> is null.</exception>
7753
/// <exception cref="ArgumentException">Thrown when <paramref name="filePath"/> is empty or whitespace.</exception>
7854
/// <exception cref="FileNotFoundException">Thrown when the specified file does not exist.</exception>
7955
/// <exception cref="COMException">Thrown when the COM operation fails.</exception>
80-
public void Load(string filePath)
56+
/// <remarks>Use <see cref="Load(string, STGM)"/> with <see cref="STGM.STGM_READWRITE"/> if you need to modify the shortcut.</remarks>
57+
public static ShellLinkFile Load(string filePath)
8158
{
82-
Load(filePath, STGM.STGM_READWRITE);
59+
return Load(filePath, STGM.STGM_READ);
8360
}
8461

8562
/// <summary>
86-
/// Loads a shortcut file with the specified storage mode.
63+
/// Loads an existing shortcut file with the specified storage mode.
8764
/// </summary>
8865
/// <param name="filePath">The path to the shortcut file to load.</param>
8966
/// <param name="storageMode">The storage mode flags.</param>
67+
/// <returns>A new <see cref="ShellLinkFile"/> instance loaded from the specified file.</returns>
9068
/// <exception cref="ArgumentNullException">Thrown when <paramref name="filePath"/> is null.</exception>
9169
/// <exception cref="ArgumentException">Thrown when <paramref name="filePath"/> is empty or whitespace.</exception>
9270
/// <exception cref="FileNotFoundException">Thrown when the specified file does not exist.</exception>
9371
/// <exception cref="COMException">Thrown when the COM operation fails.</exception>
94-
public void Load(string filePath, STGM storageMode)
72+
public static ShellLinkFile Load(string filePath, STGM storageMode)
9573
{
96-
ObjectDisposedException.ThrowIf(_disposed, this);
9774
ArgumentException.ThrowIfNullOrWhiteSpace(filePath);
98-
((IPersistFile)_shellLink).Load(filePath.ThrowIfFileDoesNotExist(), storageMode);
75+
if (!File.Exists(filePath))
76+
{
77+
throw new FileNotFoundException("The specified shortcut file does not exist.", filePath);
78+
}
79+
ShellLinkFile shellLink = new();
80+
try
81+
{
82+
((IPersistFile)shellLink._shellLink).Load(filePath, storageMode);
83+
return shellLink;
84+
}
85+
catch
86+
{
87+
shellLink.Dispose();
88+
throw;
89+
}
90+
}
91+
92+
/// <summary>
93+
/// Initializes a new instance of the <see cref="ShellLinkFile"/> class.
94+
/// </summary>
95+
private ShellLinkFile()
96+
{
97+
_shellLink = (IShellLinkW)new ShellLink();
98+
}
99+
100+
/// <summary>
101+
/// Finalizes an instance of the <see cref="ShellLinkFile"/> class.
102+
/// </summary>
103+
~ShellLinkFile()
104+
{
105+
Dispose(false);
99106
}
100107

101108
/// <summary>

0 commit comments

Comments
 (0)