检测应用程序是否固定到任务栏
Detect whether application is pinned to taskbar
我有一个 C#/WPF 应用程序,我想根据它是否从 windows 任务栏上的固定 link 启动来提供不同的行为。
- 有没有办法检测我的应用程序是否已固定到任务栏?
- 有没有办法检测我的应用程序是否已从任务栏上的固定项启动?
您可以通过检查存储所有固定应用程序快捷方式的文件夹 %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
的快捷方式,因此利用所有这些信息,可以轻松检查应用程序是否从任务栏启动。
我有一个 C#/WPF 应用程序,我想根据它是否从 windows 任务栏上的固定 link 启动来提供不同的行为。
- 有没有办法检测我的应用程序是否已固定到任务栏?
- 有没有办法检测我的应用程序是否已从任务栏上的固定项启动?
您可以通过检查存储所有固定应用程序快捷方式的文件夹 %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
的快捷方式,因此利用所有这些信息,可以轻松检查应用程序是否从任务栏启动。