Freigeben über


Gewusst wie: Abrufen der Befehlszeilenargumente

In diesem Beispiel wird gezeigt, wie die Befehlszeilenargumente, die an eine Anwendung übergeben wurden, abgerufen werden.

Beispiel

Das folgende Codebeispiel veranschaulicht die Verwendung von Application und des Startup-Ereignisses zum Abrufen der Befehlszeilenargumente.

<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>

Namespace VisualBasic
    Partial Public Class App
        Inherits Application
        Private Sub app_Startup(ByVal sender As Object, ByVal e As StartupEventArgs)
            ' If no command line arguments were provided, don't process them
            If e.Args.Length = 0 Then
                Return
            End If

            ' Get command line arguments
            For Each argument As String In e.Args
                Select Case argument
                    Case "arg1"
                        ' Process arg 1
                    Case "arg2"
                        ' Process arg 2
                    Case "arg3"
                        ' Process arg 3
                End Select
            Next argument
        End Sub
    End Class
End Namespace
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;
                }
            }
        }
    }
}