Logon Trigger

Dec 10, 2009 at 10:52 PM

I see there is a logon trigger but I don't know how to use it.


How can I set a task to run ar login?

Dec 11, 2009 at 8:37 PM
Edited Dec 11, 2009 at 9:40 PM

The logon trigger is pretty simple as it just runs right after each user logs on. See a simple sample program below. 

using System;
using Microsoft.Win32.TaskScheduler;

class Program
   static void Main(string[] args)
     new TaskService().AddTask("Test", new LogonTrigger(), new ExecAction("AnExecutable.exe", null, null));
Dec 19, 2009 at 8:51 PM
Edited Dec 19, 2009 at 8:52 PM

The above code did not work (AddTask doesn't appear to be a member function of TaskService) but I just needed to see that the LoginTrigger didn't require any special parameters (like TimeTrigger does).

When I tried using the LoginTrigger I get a System.UnauthorizedAccessException.  I do not get this if I use a different trigger.  How can I use the LoginTrigger?  Some code posted below.


TaskService test = new TaskService();
TaskDefinition td = test.NewTask();

td.RegistrationInfo.Description = "desc";

td.Principal.LogonType = TaskLogonType.InteractiveToken;

td.Triggers.Add(new LogonTrigger());

td.Actions.Add(new ExecAction(Application.StartupPath + "\\this.exe", "param", null));

test.RootFolder.RegisterTaskDefinition("stuff", td);


Dec 20, 2009 at 3:10 AM
Edited Dec 20, 2009 at 3:31 AM

You must be using an older version of the library as the AddTask method was added in version 1.3.1 (July 2009). I'm not sure why you're getting the UnauthorizedAccessException, but here are a couple of ideas. First, you may be running under an account that does not have administrator access on the machine. The second idea is that specifying the LogonType as InteractiveToken may be the problem as you are not interactive when logging on. I would try either not setting it or setting it to Password or SystemAccount and specifying the account.

Marked as answer by dahall on 9/1/2015 at 10:12 AM
Jan 6, 2014 at 11:51 PM
I just recently tried the following snippet:
new TaskService().AddTask("Test", new LogonTrigger(), new ExecAction("AnExecutable.exe", null, null));
And I get: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

I'm using the latest DLL and I'm also able to create both daily and weekly triggers.

Any ideas on why the LogonTrigger wouldn't be working?
Jan 7, 2014 at 4:02 PM
Logon triggers are kind of special cases as they are dependent on the security context of the current user. Check the link at http://msdn.microsoft.com/en-us/library/windows/desktop/aa382140(v=vs.85).aspx for more information. You likely do not have administrator rights that would allow you to add that trigger.
Marked as answer by dahall on 9/1/2015 at 10:12 AM