-
Notifications
You must be signed in to change notification settings - Fork 1k
/
ShellFile.cs
1269 lines (1141 loc) · 42.1 KB
/
ShellFile.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
// All of this code is from http://vbaccelerator.com/home/NET/Code/Libraries/Shell_Projects/Creating_and_Modifying_Shortcuts/article.asp
namespace Squirrel.Shell
{
/// <summary>
/// Summary description for ShellLink.
/// </summary>
public class ShellLink : IDisposable
{
[ComImport()]
[Guid("0000010C-0000-0000-C000-000000000046")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IPersist
{
[PreserveSig]
//[helpstring("Returns the class identifier for the component object")]
void GetClassID(out Guid pClassID);
}
[ComImport()]
[Guid("0000010B-0000-0000-C000-000000000046")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IPersistFile
{
// can't get this to go if I extend IPersist, so put it here:
[PreserveSig]
void GetClassID(out Guid pClassID);
//[helpstring("Checks for changes since last file write")]
void IsDirty();
//[helpstring("Opens the specified file and initializes the object from its contents")]
void Load(
[MarshalAs(UnmanagedType.LPWStr)] string pszFileName,
uint dwMode);
//[helpstring("Saves the object into the specified file")]
void Save(
[MarshalAs(UnmanagedType.LPWStr)] string pszFileName,
[MarshalAs(UnmanagedType.Bool)] bool fRemember);
//[helpstring("Notifies the object that save is completed")]
void SaveCompleted(
[MarshalAs(UnmanagedType.LPWStr)] string pszFileName);
//[helpstring("Gets the current name of the file associated with the object")]
void GetCurFile(
[MarshalAs(UnmanagedType.LPWStr)] out string ppszFileName);
}
[StructLayout(LayoutKind.Sequential)]
public struct PropVariant
{
public short variantType;
public short Reserved1, Reserved2, Reserved3;
public IntPtr pointerValue;
public static PropVariant FromString(string str)
{
var pv = new PropVariant() {
variantType = 31, // VT_LPWSTR
pointerValue = Marshal.StringToCoTaskMemUni(str),
};
return pv;
}
public static PropVariant FromGuid(Guid guid)
{
byte[] bytes = guid.ToByteArray();
var pv = new PropVariant() {
variantType = 72, // VT_CLSID
pointerValue = Marshal.AllocCoTaskMem(bytes.Length),
};
Marshal.Copy(bytes, 0, pv.pointerValue, bytes.Length);
return pv;
}
/// <summary>
/// Called to properly clean up the memory referenced by a PropVariant instance.
/// </summary>
[DllImport("ole32.dll")]
private extern static int PropVariantClear(ref PropVariant pvar);
/// <summary>
/// Called to clear the PropVariant's referenced and local memory.
/// </summary>
/// <remarks>
/// You must call Clear to avoid memory leaks.
/// </remarks>
public void Clear()
{
// Can't pass "this" by ref, so make a copy to call PropVariantClear with
PropVariant tmp = this;
PropVariantClear(ref tmp);
// Since we couldn't pass "this" by ref, we need to clear the member fields manually
// NOTE: PropVariantClear already freed heap data for us, so we are just setting
// our references to null.
variantType = (short)VarEnum.VT_EMPTY;
Reserved1 = Reserved2 = Reserved3 = 0;
pointerValue = IntPtr.Zero;
}
}
[StructLayout(LayoutKind.Sequential)]
public struct PROPERTYKEY
{
public Guid fmtid;
public UIntPtr pid;
public static PROPERTYKEY PKEY_AppUserModel_ID {
get {
return new PROPERTYKEY() {
fmtid = Guid.ParseExact("{9F4C2855-9F79-4B39-A8D0-E1D42DE1D5F3}", "B"),
pid = new UIntPtr(5),
};
}
}
public static PROPERTYKEY PKEY_AppUserModel_ToastActivatorCLSID {
get {
return new PROPERTYKEY() {
fmtid = Guid.ParseExact("{9F4C2855-9F79-4B39-A8D0-E1D42DE1D5F3}", "B"),
pid = new UIntPtr(26),
};
}
}
}
[ComImport]
[Guid("886D8EEB-8CF2-4446-8D02-CDBA1DBDCF99")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IPropertyStore
{
[PreserveSig]
int GetCount([Out] out uint cProps);
[PreserveSig]
int GetAt([In] uint iProp, out PROPERTYKEY pkey);
[PreserveSig]
int GetValue([In] ref PROPERTYKEY key, out PropVariant pv);
[PreserveSig]
int SetValue([In] ref PROPERTYKEY key, [In] ref PropVariant pv);
[PreserveSig]
int Commit();
}
[ComImport()]
[Guid("000214EE-0000-0000-C000-000000000046")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IShellLinkA
{
//[helpstring("Retrieves the path and filename of a shell link object")]
void GetPath(
[Out(), MarshalAs(UnmanagedType.LPStr)] StringBuilder pszFile,
int cchMaxPath,
ref _WIN32_FIND_DATAA pfd,
uint fFlags);
//[helpstring("Retrieves the list of shell link item identifiers")]
void GetIDList(out IntPtr ppidl);
//[helpstring("Sets the list of shell link item identifiers")]
void SetIDList(IntPtr pidl);
//[helpstring("Retrieves the shell link description string")]
void GetDescription(
[Out(), MarshalAs(UnmanagedType.LPStr)] StringBuilder pszFile,
int cchMaxName);
//[helpstring("Sets the shell link description string")]
void SetDescription(
[MarshalAs(UnmanagedType.LPStr)] string pszName);
//[helpstring("Retrieves the name of the shell link working directory")]
void GetWorkingDirectory(
[Out(), MarshalAs(UnmanagedType.LPStr)] StringBuilder pszDir,
int cchMaxPath);
//[helpstring("Sets the name of the shell link working directory")]
void SetWorkingDirectory(
[MarshalAs(UnmanagedType.LPStr)] string pszDir);
//[helpstring("Retrieves the shell link command-line arguments")]
void GetArguments(
[Out(), MarshalAs(UnmanagedType.LPStr)] StringBuilder pszArgs,
int cchMaxPath);
//[helpstring("Sets the shell link command-line arguments")]
void SetArguments(
[MarshalAs(UnmanagedType.LPStr)] string pszArgs);
//[propget, helpstring("Retrieves or sets the shell link hot key")]
void GetHotkey(out short pwHotkey);
//[propput, helpstring("Retrieves or sets the shell link hot key")]
void SetHotkey(short pwHotkey);
//[propget, helpstring("Retrieves or sets the shell link show command")]
void GetShowCmd(out uint piShowCmd);
//[propput, helpstring("Retrieves or sets the shell link show command")]
void SetShowCmd(uint piShowCmd);
//[helpstring("Retrieves the location (path and index) of the shell link icon")]
void GetIconLocation(
[Out(), MarshalAs(UnmanagedType.LPStr)] StringBuilder pszIconPath,
int cchIconPath,
out int piIcon);
//[helpstring("Sets the location (path and index) of the shell link icon")]
void SetIconLocation(
[MarshalAs(UnmanagedType.LPStr)] string pszIconPath,
int iIcon);
//[helpstring("Sets the shell link relative path")]
void SetRelativePath(
[MarshalAs(UnmanagedType.LPStr)] string pszPathRel,
uint dwReserved);
//[helpstring("Resolves a shell link. The system searches for the shell link object and updates the shell link path and its list of identifiers (if necessary)")]
void Resolve(
IntPtr hWnd,
uint fFlags);
//[helpstring("Sets the shell link path and filename")]
void SetPath(
[MarshalAs(UnmanagedType.LPStr)] string pszFile);
}
[ComImport()]
[Guid("000214F9-0000-0000-C000-000000000046")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IShellLinkW
{
//[helpstring("Retrieves the path and filename of a shell link object")]
void GetPath(
[Out(), MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszFile,
int cchMaxPath,
ref _WIN32_FIND_DATAW pfd,
uint fFlags);
//[helpstring("Retrieves the list of shell link item identifiers")]
void GetIDList(out IntPtr ppidl);
//[helpstring("Sets the list of shell link item identifiers")]
void SetIDList(IntPtr pidl);
//[helpstring("Retrieves the shell link description string")]
void GetDescription(
[Out(), MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszFile,
int cchMaxName);
//[helpstring("Sets the shell link description string")]
void SetDescription(
[MarshalAs(UnmanagedType.LPWStr)] string pszName);
//[helpstring("Retrieves the name of the shell link working directory")]
void GetWorkingDirectory(
[Out(), MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszDir,
int cchMaxPath);
//[helpstring("Sets the name of the shell link working directory")]
void SetWorkingDirectory(
[MarshalAs(UnmanagedType.LPWStr)] string pszDir);
//[helpstring("Retrieves the shell link command-line arguments")]
void GetArguments(
[Out(), MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszArgs,
int cchMaxPath);
//[helpstring("Sets the shell link command-line arguments")]
void SetArguments(
[MarshalAs(UnmanagedType.LPWStr)] string pszArgs);
//[propget, helpstring("Retrieves or sets the shell link hot key")]
void GetHotkey(out short pwHotkey);
//[propput, helpstring("Retrieves or sets the shell link hot key")]
void SetHotkey(short pwHotkey);
//[propget, helpstring("Retrieves or sets the shell link show command")]
void GetShowCmd(out uint piShowCmd);
//[propput, helpstring("Retrieves or sets the shell link show command")]
void SetShowCmd(uint piShowCmd);
//[helpstring("Retrieves the location (path and index) of the shell link icon")]
void GetIconLocation(
[Out(), MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszIconPath,
int cchIconPath,
out int piIcon);
//[helpstring("Sets the location (path and index) of the shell link icon")]
void SetIconLocation(
[MarshalAs(UnmanagedType.LPWStr)] string pszIconPath,
int iIcon);
//[helpstring("Sets the shell link relative path")]
void SetRelativePath(
[MarshalAs(UnmanagedType.LPWStr)] string pszPathRel,
uint dwReserved);
//[helpstring("Resolves a shell link. The system searches for the shell link object and updates the shell link path and its list of identifiers (if necessary)")]
void Resolve(
IntPtr hWnd,
uint fFlags);
//[helpstring("Sets the shell link path and filename")]
void SetPath(
[MarshalAs(UnmanagedType.LPWStr)] string pszFile);
}
[Guid("00021401-0000-0000-C000-000000000046")]
[ClassInterface(ClassInterfaceType.None)]
[ComImport()]
class CShellLink
{
}
enum EShellLinkGP : uint
{
SLGP_SHORTPATH = 1,
SLGP_UNCPRIORITY = 2
}
[Flags]
enum EShowWindowFlags : uint
{
SW_HIDE = 0,
SW_SHOWNORMAL = 1,
SW_NORMAL = 1,
SW_SHOWMINIMIZED = 2,
SW_SHOWMAXIMIZED = 3,
SW_MAXIMIZE = 3,
SW_SHOWNOACTIVATE = 4,
SW_SHOW = 5,
SW_MINIMIZE = 6,
SW_SHOWMINNOACTIVE = 7,
SW_SHOWNA = 8,
SW_RESTORE = 9,
SW_SHOWDEFAULT = 10,
SW_MAX = 10
}
[StructLayout(LayoutKind.Sequential, Pack = 4, Size = 0,
CharSet = CharSet.Unicode)]
struct _WIN32_FIND_DATAW
{
public uint dwFileAttributes;
public _FILETIME ftCreationTime;
public _FILETIME ftLastAccessTime;
public _FILETIME ftLastWriteTime;
public uint nFileSizeHigh;
public uint nFileSizeLow;
public uint dwReserved0;
public uint dwReserved1;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)] // MAX_PATH
public string cFileName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 14)]
public string cAlternateFileName;
}
[StructLayout(LayoutKind.Sequential, Pack = 4, Size = 0,
CharSet = CharSet.Ansi)]
struct _WIN32_FIND_DATAA
{
public uint dwFileAttributes;
public _FILETIME ftCreationTime;
public _FILETIME ftLastAccessTime;
public _FILETIME ftLastWriteTime;
public uint nFileSizeHigh;
public uint nFileSizeLow;
public uint dwReserved0;
public uint dwReserved1;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)] // MAX_PATH
public string cFileName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 14)]
public string cAlternateFileName;
}
[StructLayout(LayoutKind.Sequential, Pack = 4, Size = 0)]
struct _FILETIME
{
public uint dwLowDateTime;
public uint dwHighDateTime;
}
class UnManagedMethods
{
[DllImport("Shell32", CharSet = CharSet.Auto)]
internal static extern int ExtractIconEx(
[MarshalAs(UnmanagedType.LPTStr)] string lpszFile,
int nIconIndex,
IntPtr[] phIconLarge,
IntPtr[] phIconSmall,
int nIcons);
[DllImport("user32")]
internal static extern int DestroyIcon(IntPtr hIcon);
}
/// <summary>
/// Flags determining how the links with missing
/// targets are resolved.
/// </summary>
[Flags]
public enum EShellLinkResolveFlags : uint
{
/// <summary>
/// Allow any match during resolution. Has no effect
/// on ME/2000 or above, use the other flags instead.
/// </summary>
SLR_ANY_MATCH = 0x2,
/// <summary>
/// Call the Microsoft Windows Installer.
/// </summary>
SLR_INVOKE_MSI = 0x80,
/// <summary>
/// Disable distributed link tracking. By default,
/// distributed link tracking tracks removable media
/// across multiple devices based on the volume name.
/// It also uses the UNC path to track remote file
/// systems whose drive letter has changed. Setting
/// SLR_NOLINKINFO disables both types of tracking.
/// </summary>
SLR_NOLINKINFO = 0x40,
/// <summary>
/// Do not display a dialog box if the link cannot be resolved.
/// When SLR_NO_UI is set, a time-out value that specifies the
/// maximum amount of time to be spent resolving the link can
/// be specified in milliseconds. The function returns if the
/// link cannot be resolved within the time-out duration.
/// If the timeout is not set, the time-out duration will be
/// set to the default value of 3,000 milliseconds (3 seconds).
/// </summary>
SLR_NO_UI = 0x1,
/// <summary>
/// Not documented in SDK. Assume same as SLR_NO_UI but
/// intended for applications without a hWnd.
/// </summary>
SLR_NO_UI_WITH_MSG_PUMP = 0x101,
/// <summary>
/// Do not update the link information.
/// </summary>
SLR_NOUPDATE = 0x8,
/// <summary>
/// Do not execute the search heuristics.
/// </summary>
SLR_NOSEARCH = 0x10,
/// <summary>
/// Do not use distributed link tracking.
/// </summary>
SLR_NOTRACK = 0x20,
/// <summary>
/// If the link object has changed, update its path and list
/// of identifiers. If SLR_UPDATE is set, you do not need to
/// call IPersistFile::IsDirty to determine whether or not
/// the link object has changed.
/// </summary>
SLR_UPDATE = 0x4
}
public enum LinkDisplayMode : uint
{
edmNormal = EShowWindowFlags.SW_NORMAL,
edmMinimized = EShowWindowFlags.SW_SHOWMINNOACTIVE,
edmMaximized = EShowWindowFlags.SW_MAXIMIZE
}
// Use Unicode (W) under NT, otherwise use ANSI
IShellLinkW linkW;
IShellLinkA linkA;
string shortcutFile = "";
/// <summary>
/// Creates an instance of the Shell Link object.
/// </summary>
public ShellLink()
{
if (System.Environment.OSVersion.Platform == PlatformID.Win32NT)
{
linkW = (IShellLinkW)new CShellLink();
}
else
{
linkA = (IShellLinkA)new CShellLink();
}
}
/// <summary>
/// Creates an instance of a Shell Link object
/// from the specified link file
/// </summary>
/// <param name="linkFile">The Shortcut file to open</param>
public ShellLink(string linkFile) : this()
{
Open(linkFile);
}
/// <summary>
/// Call dispose just in case it hasn't happened yet
/// </summary>
~ShellLink()
{
Dispose();
}
/// <summary>
/// Dispose the object, releasing the COM ShellLink object
/// </summary>
public void Dispose()
{
if (linkW != null)
{
Marshal.ReleaseComObject(linkW);
linkW = null;
}
if (linkA != null)
{
Marshal.ReleaseComObject(linkA);
linkA = null;
}
}
public string ShortCutFile
{
get { return this.shortcutFile; }
set { this.shortcutFile = value; }
}
/// <summary>
/// Gets a System.Drawing.Icon containing the icon for this
/// ShellLink object.
/// </summary>
public Icon LargeIcon
{
get { return getIcon(true); }
}
public Icon SmallIcon
{
get { return getIcon(false); }
}
Icon getIcon(bool large)
{
// Get icon index and path:
int iconIndex = 0;
StringBuilder iconPath = new StringBuilder(260, 260);
if (linkA == null)
{
linkW.GetIconLocation(iconPath, iconPath.Capacity, out iconIndex);
}
else
{
linkA.GetIconLocation(iconPath, iconPath.Capacity, out iconIndex);
}
string iconFile = iconPath.ToString();
// If there are no details set for the icon, then we must use
// the shell to get the icon for the target:
if (iconFile.Length == 0)
{
// Use the FileIcon object to get the icon:
FileIcon.SHGetFileInfoConstants flags =
FileIcon.SHGetFileInfoConstants.SHGFI_ICON |
FileIcon.SHGetFileInfoConstants.SHGFI_ATTRIBUTES;
if (large)
{
flags = flags | FileIcon.SHGetFileInfoConstants.SHGFI_LARGEICON;
}
else
{
flags = flags | FileIcon.SHGetFileInfoConstants.SHGFI_SMALLICON;
}
FileIcon fileIcon = new FileIcon(Target, flags);
return fileIcon.ShellIcon;
}
else
{
// Use ExtractIconEx to get the icon:
IntPtr[] hIconEx = new IntPtr[1] { IntPtr.Zero };
int iconCount = 0;
if (large)
{
iconCount = UnManagedMethods.ExtractIconEx(
iconFile,
iconIndex,
hIconEx,
null,
1);
}
else
{
iconCount = UnManagedMethods.ExtractIconEx(
iconFile,
iconIndex,
null,
hIconEx,
1);
}
// If success then return as a GDI+ object
Icon icon = null;
if (hIconEx[0] != IntPtr.Zero)
{
icon = Icon.FromHandle(hIconEx[0]);
//UnManagedMethods.DestroyIcon(hIconEx[0]);
}
return icon;
}
}
/// <summary>
/// Gets the path to the file containing the icon for this shortcut.
/// </summary>
public string IconPath
{
get
{
StringBuilder iconPath = new StringBuilder(260, 260);
int iconIndex = 0;
if (linkA == null)
{
linkW.GetIconLocation(iconPath, iconPath.Capacity, out
iconIndex);
}
else
{
linkA.GetIconLocation(iconPath, iconPath.Capacity, out
iconIndex);
}
return iconPath.ToString();
}
set
{
StringBuilder iconPath = new StringBuilder(260, 260);
int iconIndex = 0;
if (linkA == null)
{
linkW.GetIconLocation(iconPath, iconPath.Capacity, out
iconIndex);
}
else
{
linkA.GetIconLocation(iconPath, iconPath.Capacity, out
iconIndex);
}
if (linkA == null)
{
linkW.SetIconLocation(value, iconIndex);
}
else
{
linkA.SetIconLocation(value, iconIndex);
}
}
}
/// <summary>
/// Gets the index of this icon within the icon path's resources
/// </summary>
public int IconIndex
{
get
{
StringBuilder iconPath = new StringBuilder(260, 260);
int iconIndex = 0;
if (linkA == null)
{
linkW.GetIconLocation(iconPath, iconPath.Capacity, out
iconIndex);
}
else
{
linkA.GetIconLocation(iconPath, iconPath.Capacity, out
iconIndex);
}
return iconIndex;
}
set
{
StringBuilder iconPath = new StringBuilder(260, 260);
int iconIndex = 0;
if (linkA == null)
{
linkW.GetIconLocation(iconPath, iconPath.Capacity, out
iconIndex);
}
else
{
linkA.GetIconLocation(iconPath, iconPath.Capacity, out
iconIndex);
}
if (linkA == null)
{
linkW.SetIconLocation(iconPath.ToString(), value);
}
else
{
linkA.SetIconLocation(iconPath.ToString(), value);
}
}
}
/// <summary>
/// Gets/sets the fully qualified path to the link's target
/// </summary>
public string Target
{
get
{
StringBuilder target = new StringBuilder(260, 260);
if (linkA == null)
{
_WIN32_FIND_DATAW fd = new _WIN32_FIND_DATAW();
linkW.GetPath(target, target.Capacity, ref fd,
(uint)EShellLinkGP.SLGP_UNCPRIORITY);
}
else
{
_WIN32_FIND_DATAA fd = new _WIN32_FIND_DATAA();
linkA.GetPath(target, target.Capacity, ref fd,
(uint)EShellLinkGP.SLGP_UNCPRIORITY);
}
return target.ToString();
}
set
{
if (linkA == null)
{
linkW.SetPath(value);
}
else
{
linkA.SetPath(value);
}
}
}
/// <summary>
/// Gets/sets the Working Directory for the Link
/// </summary>
public string WorkingDirectory
{
get
{
StringBuilder path = new StringBuilder(260, 260);
if (linkA == null)
{
linkW.GetWorkingDirectory(path, path.Capacity);
}
else
{
linkA.GetWorkingDirectory(path, path.Capacity);
}
return path.ToString();
}
set
{
if (linkA == null)
{
linkW.SetWorkingDirectory(value);
}
else
{
linkA.SetWorkingDirectory(value);
}
}
}
/// <summary>
/// Gets/sets the description of the link
/// </summary>
public string Description
{
get
{
StringBuilder description = new StringBuilder(1024, 1024);
if (linkA == null)
{
linkW.GetDescription(description, description.Capacity);
}
else
{
linkA.GetDescription(description, description.Capacity);
}
return description.ToString();
}
set
{
if (linkA == null)
{
linkW.SetDescription(value);
}
else
{
linkA.SetDescription(value);
}
}
}
/// <summary>
/// Gets/sets any command line arguments associated with the link
/// </summary>
public string Arguments
{
get
{
StringBuilder arguments = new StringBuilder(260, 260);
if (linkA == null)
{
linkW.GetArguments(arguments, arguments.Capacity);
}
else
{
linkA.GetArguments(arguments, arguments.Capacity);
}
return arguments.ToString();
}
set
{
if (linkA == null)
{
linkW.SetArguments(value);
}
else
{
linkA.SetArguments(value);
}
}
}
/// <summary>
/// Gets/sets the initial display mode when the shortcut is
/// run
/// </summary>
public LinkDisplayMode DisplayMode
{
get
{
uint cmd = 0;
if (linkA == null)
{
linkW.GetShowCmd(out cmd);
}
else
{
linkA.GetShowCmd(out cmd);
}
return (LinkDisplayMode)cmd;
}
set
{
if (linkA == null)
{
linkW.SetShowCmd((uint)value);
}
else
{
linkA.SetShowCmd((uint)value);
}
}
}
/// <summary>
/// Gets/sets the HotKey to start the shortcut (if any)
/// </summary>
public short HotKey
{
get
{
short key = 0;
if (linkA == null)
{
linkW.GetHotkey(out key);
}
else
{
linkA.GetHotkey(out key);
}
return key;
}
set
{
if (linkA == null)
{
linkW.SetHotkey(value);
}
else
{
linkA.SetHotkey(value);
}
}
}
/// <summary>
/// Sets the appUserModelId
/// </summary>
public void SetAppUserModelId(string appId)
{
var propStore = (IPropertyStore)linkW;
var pkey = PROPERTYKEY.PKEY_AppUserModel_ID;
var str = PropVariant.FromString (appId);
propStore.SetValue(ref pkey, ref str);
}
/// <summary>
/// Sets the ToastActivatorCLSID
/// </summary>
public void SetToastActivatorCLSID(string clsid)
{
var guid = Guid.Parse(clsid);
SetToastActivatorCLSID(guid);
}
/// <summary>
/// Sets the ToastActivatorCLSID
/// </summary>
public void SetToastActivatorCLSID(Guid clsid)
{
var propStore = (IPropertyStore)linkW;
var pkey = PROPERTYKEY.PKEY_AppUserModel_ToastActivatorCLSID;
var varGuid = PropVariant.FromGuid(clsid);
try {
int errCode = propStore.SetValue(ref pkey, ref varGuid);
Marshal.ThrowExceptionForHR(errCode);
errCode = propStore.Commit();
Marshal.ThrowExceptionForHR(errCode);
} finally {
varGuid.Clear();
}
}
/// <summary>
/// Saves the shortcut to ShortCutFile.
/// </summary>
public void Save()
{
Save(shortcutFile);
}
/// <summary>
/// Saves the shortcut to the specified file
/// </summary>
/// <param name="linkFile">The shortcut file (.lnk)</param>
public void Save(
string linkFile
)
{
// Save the object to disk
if (linkA == null)
{
((IPersistFile)linkW).Save(linkFile, true);
shortcutFile = linkFile;
}
else
{
((IPersistFile)linkA).Save(linkFile, true);
shortcutFile = linkFile;
}
}
/// <summary>
/// Loads a shortcut from the specified file
/// </summary>
/// <param name="linkFile">The shortcut file (.lnk) to load</param>
public void Open(
string linkFile
)
{
Open(linkFile,
IntPtr.Zero,
(EShellLinkResolveFlags.SLR_ANY_MATCH |
EShellLinkResolveFlags.SLR_NO_UI),
1);
}
/// <summary>
/// Loads a shortcut from the specified file, and allows flags controlling
/// the UI behaviour if the shortcut's target isn't found to be set.
/// </summary>
/// <param name="linkFile">The shortcut file (.lnk) to load</param>