Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi
Iam having a setup created using visual studio installer in VS 2010.

This setup iam installing to many systems on network at a time, after installing a process should be registered in task scheduler under the currently logged on user id but the process is not getting registered for some systems on network, not getting why is it happening

iam using System.Security.Principal.WindowsIdentity.GetCurrent().Name to get the currently logged on user name

when i debugged in the failed machines its getting the empty username, since iam using this username of currently logged in user to register the process in task scheduler i am not able to register the process successfully in task scheduler

The following is the code i have written in windowsforms for registering "Processforms.exe" in the task scheduler

C#
using (TaskService ts = new TaskService())
          {
              try
              {// Create a new task definition and assign properties
                  TaskDefinition td = ts.NewTask();

                  //TimeTrigger tm = new TimeTrigger();
                  //tm.Repetition.Interval = TimeSpan.FromSeconds(10);
                  td.RegistrationInfo.Description = "Monitor system activities";
                  td.Settings.StartWhenAvailable = true;

                  string user = "";
               user=System.Security.Principal.WindowsIdentity.GetCurrent().Name;

                  td.Principal.UserId = user;
                  td.Principal.RunLevel = TaskRunLevel.Highest;



                  DailyTrigger dt = new DailyTrigger();
                  dt.Repetition.Interval = TimeSpan.FromMinutes(2);

                  // Create a trigger that will fire the task at this time every other day
                  td.Triggers.Add(dt);
                  td.Triggers.Add(new BootTrigger());

                  // Create an action that will launch Notepad whenever the trigger fires
                  td.Actions.Add(new ExecAction(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ProcessForms.exe"), "", null));

                  // Register the task in the root folder
                  ts.RootFolder.RegisterTaskDefinition(@"ProcessFormsTask", td);


                  Task tsk = ts.FindTask("ProcessFormsTask");


                  if (tsk != null)
                  {

                      tsk.Run();
                  }

                  // Remove the task we just created
                  //   ts.RootFolder.DeleteTask("Test");
              }
              catch (Exception ex)
              {
                  try
                  {

                      string fileloc = Path.Combine(@"C:\", "ORCAActivityError");
                      if (!Directory.Exists(fileloc))
                      {
                          Directory.CreateDirectory(fileloc);
                      }
                      string filepath = Path.Combine(fileloc, "logfile.txt");
                      using (StreamWriter sw = new StreamWriter(filepath, true))
                      {
                          sw.Write(ex.Message + "   " + DateTime.Now.ToString());
                      }

                  }
                  catch { }
              }
          }


please help me with the problem

Thanks in advance
Posted
Updated 16-Sep-15 20:23pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900