简体   繁体   中英

Launch program after each login

I have tried to find this everywhere on the internet with no luck. I want to make my program launch after every login (LoginUI.exe). Is it possible to detect when the user has locked his/her computer (Winkey + L) and then launch my program? If this is not possible, then is there a way to detect once a user has just logged in?

You could write a program that monitors the user session state via SystemEvents in 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;
  }
}

In your case, you could do Process.Start(...) when you detect either SessionLogon or SessionUnlock .

Looks like SO already has some information on this type of thing. A registry modification in c# looks like it will do the trick.

Programmatically start application on login

I think this is what you are looking for man! Here is the relevant snippet:

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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM