我有一个C#/ WPF应用程序,根据它是否已从Windows任务栏上的固定链接启动,我希望给出不同的行为。
您可以通过检查%appdata%\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar
存储所有固定应用程序快捷方式的文件夹,来检测当前用户是否将应用程序固定到任务栏。例如(需要添加对Windows脚本宿主对象模型的COM引用):
private static bool IsCurrentApplicationPinned() {
// path to current executable
var currentPath = Assembly.GetEntryAssembly().Location;
// folder with shortcuts
string location = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar");
if (!Directory.Exists(location))
return false;
foreach (var file in Directory.GetFiles(location, "*.lnk")) {
IWshShell shell = new WshShell();
var lnk = shell.CreateShortcut(file) as IWshShortcut;
if (lnk != null) {
// if there is shortcut pointing to current executable - it's pinned
if (String.Equals(lnk.TargetPath, currentPath, StringComparison.InvariantCultureIgnoreCase)) {
return true;
}
}
}
return false;
}
还有一种方法可以检测是否从固定的项目启动了应用程序。为此,您将需要GetStartupInfo
win api函数。除其他信息外,它将为您提供开始当前进程的快捷方式(或仅文件)的完整路径。例:
[DllImport("kernel32.dll", SetLastError = true, EntryPoint = "GetStartupInfoA")]
public static extern void GetStartupInfo(out STARTUPINFO lpStartupInfo);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct STARTUPINFO
{
public uint cb;
public string lpReserved;
public string lpDesktop;
public string lpTitle;
public uint dwX;
public uint dwY;
public uint dwXSize;
public uint dwYSize;
public uint dwXCountChars;
public uint dwYCountChars;
public uint dwFillAttribute;
public uint dwFlags;
public ushort wShowWindow;
public ushort cbReserved2;
public IntPtr lpReserved2;
public IntPtr hStdInput;
public IntPtr hStdOutput;
public IntPtr hStdError;
}
用法:
STARTUPINFO startInfo;
GetStartupInfo(out startInfo);
var startupPath = startInfo.lpTitle;
现在,如果您已经从任务栏启动了应用程序,startupPath
它将指向的快捷方式%appdata%\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar
,因此使用所有这些信息,很容易检查应用程序是否从任务栏启动。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句