C# - Arguments for application
How can I make it so when ther开发者_开发百科e 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
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);
}
}
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
Command line arguments.
On c# you can find them on
static void Main(string[] args)
Or from anywhere using
Environment.GetCommandLineArgs()
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
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
精彩评论