我正在尝试以编程方式查找为给定 MSI lnk 文件(广告快捷方式)运行的 exe 文件。我使用了类似于 Is there a way to resolve a .lnk target that works for links that end up in c:\windows\installer? 的答案中显示的方法。 .这种方法适用于大多数 MSI lnk 文件。不幸的是,有少数 lnk 文件运行良好,但 MsiGetShortcutTarget 不返回组件 ID。因此,对 MsiGetComponentPath 的后续调用将返回 InvalidArg。
这是我正在使用的代码(取自 here ):
public const int MaxFeatureLength = 38;
public const int MaxGuidLength = 38;
public const int MaxPathLength = 1024;
public static string ParseShortcut(string shortcutFilename)
{
StringBuilder product = new StringBuilder(MaxGuidLength + 1);
StringBuilder feature = new StringBuilder(MaxFeatureLength + 1);
StringBuilder component = new StringBuilder(MaxGuidLength + 1);
var returnValue = MsiGetShortcutTarget(shortcutFilename, product, feature, component);
if (returnValue != 0)
{
return null;
}
int pathLength = MaxPathLength;
StringBuilder path = new StringBuilder(pathLength);
InstallState installState = MsiGetComponentPath(product.ToString(), component.ToString(), path, ref pathLength);
if (installState == InstallState.Local)
{
return path.ToString();
}
else
{
return null;
}
}
我机器上的一个例子是 C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Microsoft Office 2013\Office 2013 Tools\Office 2013 Language Preferences.lnk
产品编号:{91150000-0011-0000-0000-0000000FF1CE} 功能 ID:SetLanguageFiles
我相信 IShellLink 接口(interface)不能用于为 MSI Advertised Shortcuts 返回可运行的 exe,而我这样做的尝试返回了包含图标资源的 exe 的路径。
很明显,操作系统可以找到合适的 exe(在本例中为 C:\Program Files (x86)\Microsoft Office\Office15\SETLANG.EXE)。
如何使用代码获取与此 lnk 文件相关联的 exe 文件?
最佳答案
当 MsiGetShortcutTarget 没有返回组件 ID 时,可以从 MSI 数据库中获取组件 ID。一种方法是使用 WiX DTF,如下所示:https://stackoverflow.com/a/34680682/3051702 .
然后可以在调用 MsiGetComponentPath 时使用返回的组件 ID。或者,可以直接使用本地方法。
关于c# - MsiGetShortcutTarget MSI LNK 文件不返回组件 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34638433/