- 关 键 词:
- .net framework
本教程展示如何访问命令行以及访问命令行参数数组的两种方法。
教程
下面的示例展示使用传递给应用程序的命令行参数的两种不同方法。
示例 1
本示例演示如何输出命令行参数。
// cmdline1.cs
// arguments: A B C
using System;
public class CommandLine
{
public static void Main(string[] args)
{
// The Length property is used to obtain the length of the array.
// Notice that Length is a read-only property:
Console.WriteLine("Number of command line parameters = {0}",
args.Length);
for(int i = 0; i < args.Length; i++)
{
Console.WriteLine("Arg[{0}] = [{1}]", i, args[i]);
}
}
}
输出
使用如下所示的一些参数运行程序:cmdline1 A B C。
输出将为:
Number of command line parameters = 3
Arg[0] = [A]
Arg[1] = [B]
Arg[2] = [C]
示例 2
循环访问数组的另一种方法是使用 foreach 语句,如本示例所示。foreach 语句可用于循环访问数组或“.NET Framework”集合类。它提供了一种简单的方法来循环访问集合。
// cmdline2.cs
// arguments: John Paul Mary
using System;
public class CommandLine2
{
public static void Main(string[] args)
{
Console.WriteLine("Number of command line parameters = {0}",
args.Length);
foreach(string s in args)
{
Console.WriteLine(s);
}
}
}
输出
使用如下所示的一些参数运行程序:cmdline2 John Paul Mary。
输出将为:
Number of command line parameters = 3
John
Paul
Mary
相关专题
- Linux命令简介 (9649篇文章)
- ASP.NET教程 (8343篇文章)
- FreeBSD使用教程 (6512篇文章)
- C#版的网站新闻发布系统 (690次浏览)
- VC#初学入门:第一个Windows程序 (536次浏览)
- C# WinForm中DataGrid列设置 (534次浏览)
- C#程序设计入门经典之C#的基本语法 (526次浏览)
- C# 编程规范 (519次浏览)
- .net中webform和winform连接sql server 2000 (316次浏览)
- 如何向某网址Post信息,并直接通过验证 (304次浏览)
- 多线程在Visual C#网络编程中的应用(1) (298次浏览)
- 获取主机的IP地址 (170次浏览)
- C# 参考之访问关键字:base、this (144次浏览)



