每次登录后启动程序

本文关键字:启动 程序 登录 | 更新日期: 2023-09-27 17:54:57

我试着在互联网上到处找到这个,没有运气。我想让我的程序启动后,每次登录(LoginUI.exe)。是否有可能检测到用户何时锁定了他/她的计算机(Winkey + L),然后启动我的程序?如果这是不可能的,那么是否有一种方法来检测一旦用户刚刚登录?

每次登录后启动程序

您可以编写一个程序,通过Microsoft.Win32中的SystemEvents监视用户会话状态:

// Put this somewhere in your console app/windows form initialization code.
SystemEvents.SessionSwitch += OnSessionSwitch;
// Put this method in your console app/windows form somewhere.
static void OnSessionSwitch(object sender, SessionSwitchEventArgs e)
{
  switch (e.Reason)
  {
    case SessionSwitchReason.SessionLogon:
      // User has logged on to the computer.
      break;
    case SessionSwitchReason.SessionLogoff:
      // User has logged off from the computer.
      break;
    case SessionSwitchReason.SessionUnlock:
      // The computer has been unlocked.
      break;
    case SessionSwitchReason.SessionLock:
      // The computer has been locked.
      break;
  }
}

在您的情况下,当您检测到SessionLogonSessionUnlock时,您可以执行Process.Start(...)

看起来SO已经有了一些关于这类事情的信息。在c#中修改注册表似乎可以达到这个目的。

登录时编程启动应用程序

我想这就是你要找的人!下面是相关的代码片段:

WshShell shell = new WshShell();
string shortcutAddress = startupFolder + @"'MyStartupShortcut.lnk";
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutAddress);
shortcut.Description = "A startup shortcut. If you delete this shortcut from your computer, LaunchOnStartup.exe will not launch on Windows Startup"; // set the description of the shortcut
shortcut.WorkingDirectory = Application.StartupPath; /* working directory */
shortcut.TargetPath = Application.ExecutablePath; /* path of the executable */
shortcut.Save(); // save the shortcut