Autor Beitrag
riro
Hält's aus hier
Beiträge: 6



BeitragVerfasst: Sa 26.07.08 17:43 
Wie kann ich in einer C# Konsolenanwendung Variablen beim Programmaufruf als Parameter definieren?

So das wenn man das Programm z.B. so in der Eingabeaufforderung startet:

programm.exe wert1 wert2 wert3

wert1 als Variable1 definiert wird u.s.w.
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Sa 26.07.08 17:47 
Hallo!

Die Main-Methode hat den Parameter args, der enthält die Kommandozeilen-Parameter. Bin mir gerade nicht sicher, aber es kann sein, dass das erste Element des Arrays der Programmname selber ist :gruebel:

Grüße
Christian

//edit: Ach ja, :welcome:

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
lothi
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 114
Erhaltene Danke: 3



BeitragVerfasst: So 17.08.08 22:23 
Hallo

Ich glaube nicht. :oops: :roll:

Wenn ich das ausführe ohne einen Parameter gib's eine IndexOutOfRangeException:

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
  class Program
  {
    static void Main(string[] args)
    {
      Console.WriteLine(args[0]);
    }
  }
}


Gruss Lothi
Greenberet
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 339
Erhaltene Danke: 20

Win 10
C# (VS 2012), C++ (VS 2012/GCC), PAWN(Notepad++), Java(NetBeans)
BeitragVerfasst: So 17.08.08 22:36 
jo in args stehen nur die parameter drinn wenn auch welche übergeben werden.
Falls du den dateinamen brauchst -> "Assembly.GetExecutingAssembly().Location" (System.Reflection)
Kha
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 3803
Erhaltene Danke: 176

Arch Linux
Python, C, C++ (vim)
BeitragVerfasst: Mo 18.08.08 16:14 
Und die originale Kommandozeile (Exe + Params) findet sich in Environment.CommandLine.