注意:在使用如下代码之前需进行这些操作:选择“解决方案资源管理”---“引用”---“右键”---“添加引用”---“COM”---“Windows Script Host Object Model”---“确定”。
//代码如下:
//获取快捷方式指向的目标路径
public static readonly Guid CLSID_WshShell = new Guid("72C24DD5-D70A-438B-8A42-98424B88AFB8");
public static string GetShortCutTarget(string lnk) // 取快捷方式目标
{
if (lnk != null && System.IO.File.Exists(lnk))
{
dynamic objWshShell = null, objShortcut = null;
try
{
objWshShell = Activator.CreateInstance(Type.GetTypeFromCLSID(CLSID_WshShell));
objShortcut = objWshShell.CreateShortcut(lnk);
Console.WriteLine(objShortcut.TargetPath);
return objShortcut.TargetPath;
}
finally
{
Marshal.ReleaseComObject(objShortcut);
Marshal.ReleaseComObject(objWshShell);
}
}
return string.Empty;
}