【发布时间】:2017-12-01 22:30:33
【问题描述】:
我正在尝试设置指向文件夹的快捷方式图标,但找不到任何有关如何从 shell32.dll 将快捷方式图标设置为本机图标的资源。我在 Rykler 的 msdn 上找到了This 答案,但答案已经过时了。任何帮助将不胜感激。
代码
SHFILEINFO shinfo = new SHFILEINFO();
Win32.SHGetFileInfo(filename, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), (int)0x1000);
// icon index # and path
int IconIndex = shinfo.iIcon
string IconPath = shinfo.szDisplayName
shortcut.IconLocation = ???
SHFILEINFO 结构:(取自This问题)
[StructLayout(LayoutKind.Sequential)]
public struct SHFILEINFO
{
public IntPtr hIcon;
public int iIcon;
public uint dwAttributes;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string szDisplayName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
public string szTypeName;
};
class Win32
{
[DllImport("shell32.dll")]
public static extern IntPtr SHGetFileInfo(string pszPath, uint
dwFileAttributes, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags);
}
【问题讨论】:
-
"但 Visual Studio 抛出错误" 任何时候出现错误消息时,最好将其作为问题的一部分进行分享。
-
“ExpandEnvironmentStrings”方法没有重载需要 2 个参数。我认为我正在查看的答案已经过时了。
-
ExpandEnvironmentStrings没有出现在您的代码中,那怎么会出现在错误中? -
msdn 上列出的答案使用
ExpandEnvironmentStrings。 -
也许这个库会对你有所帮助:github.com/securifybv/ShellLink