每次登录后启动程序

时间:2013-01-19 22:35:16

标签: c# windows winforms login

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

3 个答案:

答案 0 :(得分:5)

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

// 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;
  }
}

在您的情况下,当您检测到Process.Start(...)SessionLogon时,您可以SessionUnlock

答案 1 :(得分:0)

看起来SO已经有关于此类事情的一些信息。 c#中的注册表修改看起来就像是可以解决问题。

Programmatically start application on login

答案 2 :(得分:0)

我认为this正是你在找人的!以下是相关摘录:

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