-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathShellLink.cs
More file actions
113 lines (92 loc) · 3.96 KB
/
Copy pathShellLink.cs
File metadata and controls
113 lines (92 loc) · 3.96 KB
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
using System;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
namespace avUpload
{
/// <summary>
/// Erstellt Windows-Verknüpfungen (.lnk) ohne COM-Referenz auf IWshRuntimeLibrary.
/// Verwendet die native Shell32-Schnittstelle IShellLink direkt per P/Invoke.
/// </summary>
internal static class ShellLink
{
public static void CreateShortcut(
string lnkPath,
string targetPath,
string workingDirectory,
string description)
{
var link = (IShellLink)new CShellLink();
link.SetPath(targetPath);
link.SetWorkingDirectory(workingDirectory);
link.SetDescription(description);
((IPersistFile)link).Save(lnkPath, fRemember: false);
}
// ------------------------------------------------------------------ //
// Native COM-Interfaces – kein separates Assembly nötig
// ------------------------------------------------------------------ //
[ComImport]
[Guid("00021401-0000-0000-C000-000000000046")]
private class CShellLink { }
[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("000214F9-0000-0000-C000-000000000046")]
private interface IShellLink
{
void GetPath(
[Out, MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder pszFile,
int cch,
out WIN32_FIND_DATA pfd,
uint fFlags);
void GetIDList(out IntPtr ppidl);
void SetIDList(IntPtr pidl);
void GetDescription(
[Out, MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder pszName,
int cchMaxName);
void SetDescription(
[MarshalAs(UnmanagedType.LPWStr)] string pszName);
void GetWorkingDirectory(
[Out, MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder pszDir,
int cchMaxPath);
void SetWorkingDirectory(
[MarshalAs(UnmanagedType.LPWStr)] string pszDir);
void GetArguments(
[Out, MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder pszArgs,
int cchMaxPath);
void SetArguments(
[MarshalAs(UnmanagedType.LPWStr)] string pszArgs);
void GetHotkey(out short pwHotkey);
void SetHotkey(short wHotkey);
void GetShowCmd(out int piShowCmd);
void SetShowCmd(int iShowCmd);
void GetIconLocation(
[Out, MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder pszIconPath,
int cchIconPath,
out int piIcon);
void SetIconLocation(
[MarshalAs(UnmanagedType.LPWStr)] string pszIconPath,
int iIcon);
void SetRelativePath(
[MarshalAs(UnmanagedType.LPWStr)] string pszPathRel,
uint dwReserved);
void Resolve(IntPtr hwnd, uint fFlags);
void SetPath(
[MarshalAs(UnmanagedType.LPWStr)] string pszFile);
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
private struct WIN32_FIND_DATA
{
public uint dwFileAttributes;
public System.Runtime.InteropServices.ComTypes.FILETIME ftCreationTime;
public System.Runtime.InteropServices.ComTypes.FILETIME ftLastAccessTime;
public System.Runtime.InteropServices.ComTypes.FILETIME ftLastWriteTime;
public uint nFileSizeHigh;
public uint nFileSizeLow;
public uint dwReserved0;
public uint dwReserved1;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string cFileName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 14)]
public string cAlternateFileName;
}
}
}