0

I am creating a job upload program in c#. I have all the job information maintained in the config file.

When I create a job, I do pass an unique argument for that job.

using (TaskService ts = new TaskService())
{
   TaskDefinition td = ts.NewTask();
   TimeTrigger trigger = new TimeTrigger();
   var startTime = TimeSpan.Parse(section1["ScheduledTime"]);
                            trigger.StartBoundary = DateTime.Today + startTime;
   trigger.Repetition.Interval = TimeSpan.FromDays(1);
   trigger.StartBoundary = DateTime.Now;
   trigger.Id = "XXX";
   trigger.Repetition.Interval = TimeSpan.FromDays(1);
   td.Triggers.Add(trigger);
   td.Actions.Add(new ExecAction(@"C:/JobUpload.exe", argument, ""));
   var foldername = ts.GetFolder(@"\Data");
   foldername.RegisterTaskDefinition(section1["JobName"], td);
 }

In the program, I have to check whether the job has any arguments. If no arguments are present (this occurs when the exe file is triggered manually), then I have to trigger the method A.

On the other hand, if the exe file is triggered through the job in task scheduler, then it has arguments associated with it, so I have to trigger the method B.

I am not sure on how to check whether a job has any arguments in C#.

Any help would be appreciated.

Edit:

if (args.Length > 0)
{
  Console.WriteLine("more than 1");
  Logger.Info("More than 1");
  Debug.WriteLine("More than 1");
}
user3447653
  • 3,968
  • 12
  • 58
  • 100
  • What development platform you are using? WPF? Forms? – Kinani Jun 21 '16 at 20:09
  • I am just using a console application. – user3447653 Jun 21 '16 at 20:10
  • Did you check [this thread](http://stackoverflow.com/questions/11791969/c-sharp-check-if-you-have-passed-arguments-or-not)? Your question is related to arguments to console application whereas all the content and the title is related to task scheduling, totally irrelevant. Read this: [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask) – Techie Jun 21 '16 at 20:14

2 Answers2

0

In your console application, there should be an Program.cs with a static void Main(string[] args) method.

Any arguments given through command line would be passed in to the args parameter. If no arguments are passed in, the args.Length would be 0.

See Command-Line Arguments (C# Programming Guide)

Dhwanil Shah
  • 1,072
  • 1
  • 9
  • 25
0

C# Console Application Entry Point is the Main Method.

static int Main(string[] args)

as you can see there's a parameter to that Method, which is the arguments that's passed to your application.

You can access them as any normal array.

if(args[0] == "X")
   MethodOne();
else
   MethodTwo();
Kinani
  • 484
  • 1
  • 4
  • 19
  • This works when I run the program through command line. But when the .exe file gets triggered, I am providing the argument name through "Add arguments (optional)" in Task scheduler. But that does not get captured in args[0]. Not sure where it gets captured. – user3447653 Jun 21 '16 at 23:18
  • When using Task Scheduler, args.Length == 0? Log the output to check for it. – Kinani Jun 22 '16 at 00:12
  • I am new to C#, I have edited the code in the question. But it does not print anything when i manually run the job from task scheduler. – user3447653 Jun 22 '16 at 00:23