| Ylbtech-microsoft-csharpsamples:ylbtech-languagesamples-commandline (Command line arguments) |
| 1.A, example (sample) back to top |
Command line Arguments sample
This example shows how to access the command line and shows two ways to access the array of command-line arguments.
| Safety Instructions |
This code example is provided to illustrate a concept that does not represent the most secure coding practice, so you should not use this code example in an application or Web site. Microsoft assumes no responsibility for incidental or consequential damages arising from the use of this code sample for other purposes. |
To build and run the command line arguments sample in Visual Studio
Open the solution (CommandLine.sln).
In Solution Explorer, right-click the CMDLINE1 project and select Set as Startup Project.
In Solution Explorer, right-click the project and then select Properties.
Open the Configuration Properties folder and click Debug.
In the command line Arguments property, type the command-line arguments (for an example, see the Tutorial) and click OK.
On the Debug menu, click Start Without Debugging.
For CmdLine2, repeat the previous steps.
To build and run the command line arguments sample from the command line
Use the "Change Directory" command to go to the "CmdLine1" directory.
Type the following command:
csc cmdline1.cscmdline1 A B C |
Use the "Change Directory" command to go to the "CmdLine2" directory.
Type the following command:
csc cmdline2.cscmdline2 John Paul Mary |
| 1.B, sample code back to top |
1.b.1, Cmdline1.cs
//Copyright (C) Microsoft Corporation. All rights reserved. //This code is published in compliance with//Microsoft Public License (MS-PL,http://opensource.org/licenses/ms-pl.html) of the terms. ////Copyright (C) Microsoft Corporation. All rights reserved. //Cmdline1.cs//parameter: A B CusingSystem; Public classcommandline{ Public Static voidMain (string[] args) { //The Length property is used to get the lengths of the array. //Note that Length is a read-only property:Console.WriteLine ("Number of command line parameters = {0}", args. Length); for(inti =0; I < args. Length; i++) {Console.WriteLine ("arg[{0}] = [{1}]", I, args[i]); } }}View Code
1.b.2,
| 1.B, sample code 2 (sample codes) back to top |
1.b.1, Cmdline2.cs
//Copyright (C) Microsoft Corporation. All rights reserved. //This code is published in compliance with//Microsoft Public License (MS-PL,http://opensource.org/licenses/ms-pl.html) of the terms. ////Copyright (C) Microsoft Corporation. All rights reserved. //Cmdline2.cs//parameters: John Paul MaryusingSystem; Public classcommandline2{ Public Static voidMain (string[] args) {Console.WriteLine ("Number of command line parameters = {0}", args. Length); foreach(stringSinchargs) {Console.WriteLine (s); } }}View Code
1.b.2,
| 1.C, (free Download) back to top |
|
Ylbtech Source: http://ylbtech.cnblogs.com/ This article is copyright to the author and the blog Park, Welcome to reprint, but without the consent of the author must retain this paragraph, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility. |
Ylbtech-languagesamples-commandline (Command-line arguments)