NateShoffner
NateShoffner

Reputation: 16829

C# - Arguments for application

How can I make it so when there are arguments added to the end of the program name it does a specific method or whatever?

Also, is there a name for this?

Example:

program.exe /i

I've also seen %1

Upvotes: 0

Views: 1107

Answers (5)

Joey
Joey

Reputation: 354536

There are a few things you mention here.

First of all, you want command-line arguments. How you get them depends on the type of application. For example in a console application you define the main method like this:

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

where you can access all command-line arguments that were given to the program in the args array.

In other project types you may need to resort to Environment.GetCommandLineArgs.

Furthermore, you talk about %1 which has, at first, nothing to do with your specific problem here. It's used in batch files and in the registry when setting file type associations. It stands for the first command-line argument in batches, or the document you want to open for file type associations.

So when setting a file type association for your program you may use the following commands (on the Windows command line):

assoc .myExt=MyProgram
ftype MyProgram=myprogram.exe /i %1

Upvotes: 2

Andrew Keith
Andrew Keith

Reputation: 7563

here is a snippet

class myclass
{
  public static void main(string [] args)
  {
    if(args.Length == 1)
    {
       if(args[0] == "/i")
       {
         Console.WriteLine("Parameter i");
       }
    }
  }
}

The %1 is actually syntax for BAT files to pass the parameter through. So if you see program.exe %1 in a file named cmd.bat, you can call cmd.bat /i and the /i will be passed through to program.exe

Upvotes: 5

bzlm
bzlm

Reputation: 9727

These are called command-line arguments. There's a good tutorial on MSDN on how to use them.

This example should get you started:

class TestClass
{
    static void Main(string[] args)
    {
        // Display the number of command line arguments:
        System.Console.WriteLine(args.Length);
    }
}

Upvotes: 6

user170442
user170442

Reputation:

You are looking for commandline arguments, aren't You?

Here You will find some examples: http://www.csharphelp.com/archives/archive273.html Here more: http://www.google.com/search?hl=en&q=%22c%23%22+command+line+arguments&aq=f&oq=&aqi=g10

Upvotes: 2

Kobi
Kobi

Reputation: 138017

Command line arguments.

On c# you can find them on

static void Main(string[] args)

Or from anywhere using

Environment.GetCommandLineArgs()

Upvotes: 2

Related Questions