-->
You can send arguments to the Main
method by defining the method in one of the following ways:
Note
To run an executable in PowerShell, you just need to specify its name. This is the same as running an executable in Cmd.exe. For example, Figure 1 shows two examples of running ShowArgs.exe directly in PowerShell. In Figure 1, the. Prefix is needed to run ShowArgs.exe because PowerShell doesn't run executables from the current directory. If you want it to run automatically, place some commands in a file (e.g. 'run') and give it as argument: -x /tmp/cmds. Optionally you can run with -batch mode. Gdb -batch -x /tmp/cmds -args executablename arg1 arg2 arg3 share improve this answer.
To enable command-line arguments in the Main
method in a Windows Forms application, you must manually modify the signature of Main
in program.cs. The code generated by the Windows Forms designer creates a Main
without an input parameter. You can also use Environment.CommandLine or Environment.GetCommandLineArgs to access the command-line arguments from any point in a console or Windows application.
The parameter of the Main
method is a String array that represents the command-line arguments. Usually you determine whether arguments exist by testing the Length
property, for example:
You can also convert the string arguments to numeric types by using the Convert class or the Parse
method. For example, the following statement converts the string
to a long
number by using the Parse method:
It is also possible to use the C# type long
, which aliases Int64
:
You can also use the Convert
class method ToInt64
to do the same thing:
For more information, see Parse and Convert.
Example
The following example shows how to use command-line arguments in a console application. The application takes one argument at run time, converts the argument to an integer, and calculates the factorial of the number. If no arguments are supplied, the application issues a message that explains the correct usage of the program.
To compile and run the application from a command prompt, follow these steps:
Run Exe With Arguments In Batch File
Paste the following code into any text editor, and then save the file as a text file with the name
Factorial.cs
.From the Start screen or Start menu, open a Visual Studio Developer Command Prompt window, and then navigate to the folder that contains the file that you just created.
Enter the following command to compile the application.
csc Factorial.cs
If your application has no compilation errors, an executable file that's named
Factorial.exe
is created.Enter the following command to calculate the factorial of 3:
Factorial 3
The command produces this output:
The factorial of 3 is 6.
Run Exe With Arguments Batch
Note
Run Exe With Arguments Vbscript
When running an application in Visual Studio, you can specify command-line arguments in the Debug Page, Project Designer.