隨風(fēng)設(shè)計

          隨風(fēng)設(shè)計

            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            11 隨筆 :: 0 文章 :: 0 評論 :: 0 Trackbacks

          本教程展示如何訪問命令行以及訪問命令行參數(shù)數(shù)組的兩種方法。
          教程
              下面的示例展示使用傳遞給應(yīng)用程序的命令行參數(shù)的兩種不同方法。

          示例 1
              本示例演示如何輸出命令行參數(shù)。
              // 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]);
                    }
                 }
              }
          輸出
              使用如下所示的一些參數(shù)運行程序:cmdline1 A B C。
               輸出將為:
              Number of command line parameters = 3
              Arg[0] = [A]
              Arg[1] = [B]
              Arg[2] = [C]
          示例 2
              循環(huán)訪問數(shù)組的另一種方法是使用 foreach 語句,如本示例所示。foreach 語句可用于循環(huán)訪問數(shù)組或“.NET Framework”集合類。它提供了一種簡單的方法來循環(huán)訪問集合。
              // 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);
                    }
                  }
               }
          輸出
              使用如下所示的一些參數(shù)運行程序:cmdline2 John Paul Mary。

              輸出將為:
              Number of command line parameters = 3
              John
              Paul
              Mary

          posted on 2006-08-17 11:27 曹賢 閱讀(281) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 建平县| 闽侯县| 明水县| 青川县| 绥阳县| 苍山县| 正镶白旗| 准格尔旗| 阳泉市| 奉贤区| 仙桃市| 隆德县| 南陵县| 翁源县| 雅安市| 理塘县| 子长县| 三亚市| 远安县| 芜湖县| 紫云| 湛江市| 宁都县| 隆化县| 五家渠市| 三门峡市| 沛县| 县级市| 乐安县| 三都| 汝州市| 乃东县| 沈阳市| 江永县| 佛学| 万安县| 成武县| 宽城| 乌海市| 阜阳市| 松溪县|