3

Its really really hard to explain what i am trying to do, so i will just explain what is the program i am trying to build.

I am building a Console App in C#.

I need to run it every day at 6:00. I have already built the timer for it, but i need it to get parameter with a "Name" like when you run it into PowerShell. I need to do something like that:

.\run.exe Name

which in my program I need the name in a string, and write with it.. and use it ofc. i will like to know how to add it, so i can do even:

.\run.exe Name Name2 Name3

Thanks again everyone, you are helping me here alot!

4 Answers 4

3

Your Console App entry point may look like

public static void Main(string[] args) { }

the args array contains all parameters you have passed via command line. The different elements are those that were separated by space.

So if you call your prog like prog.exe Name, args[0] will contain the value "Name". args.Length will have the value 1 . If you call prog.exe Name X, args.Length will be 2 and args[1] will be "X".

Sign up to request clarification or add additional context in comments.

3 Comments

here's an article on how to work with arrays: msdn.microsoft.com/en-us/library/aa288453%28v=vs.71%29.aspx
leave it. Thanks again :) i found it. THANKSKKKSS!
args[whatever index name is].
0

What you want is here: http://msdn.microsoft.com/en-us/library/acy3edy3.aspx

if (args.Length == 0)
{
    System.Console.WriteLine("Please enter a numeric argument.");
    return 1;
}
else if(args.Length == 1)
{
//do stuff here
}

etc

Comments

0

Pretty simple:

static void Main(string[] args)
{
  // ... use args[] to get at your arguments...
}

Comments

0

That is not a problem. The arguments you supply are available. Try this:

 static void Main(string[] args)
    {

        for (int i = 0; i < args.Length; i++)
        {
            Console.WriteLine(args[i]);
        }
     }

if you want name, you might want to do something like

program.exe  /name=name /brand=brand /three=somethingelse

and parse the args for the /name=

This way you have more control over the arguments and the user can provide them in random order.

1 Comment

When parsing the arguments, is the entire value of the argument args[0] "/name=name" (in the 1st parameter in your example)? So I have to split the string and confirm the left side is the name of the parameter and the right is a valid value?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.