如何:获取命令行参数

更新:2007 年 11 月

本示例演示如何获取已传递给应用程序的命令行参数。

示例

下面的代码演示如何使用 ApplicationStartup 事件获取命令行参数。

<Application
    x:Class="CSharp.App"
    xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
    Startup="app_Startup"
    >
</Application>
using System;
using System.Windows;

namespace CSharp
{
    public partial class App : Application
    {
        void app_Startup(object sender, StartupEventArgs e)
        {
            // If no command line arguments were provided, don't process them
            if (e.Args.Length == 0) return;

            // Get command line arguments
            foreach (string argument in e.Args)
            {
                switch (argument)
                {
                    case "arg1":
                        // Process arg 1
                        break;
                    case "arg2":
                        // Process arg 2
                        break;
                    case "arg3":
                        // Process arg 3
                        break;
                }
            }
        }
    }
}

有关检索和使用命令行参数的示例,请参见处理命令行参数的示例

请参见

任务

处理命令行参数的示例