/ / Pasar parámetros en un método, en una instrucción if-else C # - c #, instrucción if, métodos, pasar parámetros, argumentos de línea de comandos

Pasar parámetros en un método, en una instrucción if-else C # - c #, instrucción if, métodos, paso de parámetros, argumentos de línea de comando

Estoy escribiendo un método que puede tomar ciertonúmero de parámetros y contiene una instrucción if-else. Cuando uso argumentos de la línea de comandos, paso un mínimo de 3 parámetros y un máximo de 4.

Cuando ejecuto mi línea de comando con solo 3 parámetrossolo debe ejecutar la primera parte del if. Solo cuando tenga que pasar el cuarto parámetro, se ejecutará de otra manera, sin embargo, cada vez que ejecuto cuatro parámetros, el código nunca llega a lo demás y solo ejecuta el comienzo de la instrucción if. Cualquier idea es apreciada

protected void net_group(string command, string param1, string param2, string param3)
{


Console.WriteLine("Got information net group command");

//creates group.txt file when "net group" command is used
string path = "C:\Files\groups.txt";
using (StreamWriter sw = File.AppendText(path))
{
sw.WriteLine(param2 + ": " + param3); //param2 is name of the group //param3 is name of user
}
if (not sure what argument would go here) {

//writes to the audit log and to the console when group is made w/out users
Console.WriteLine("Group " + param2 + " created");
string path2 = "C:\Files\audit.txt";
using (StreamWriter sw2 = File.AppendText(path2))
{
sw2.WriteLine("Group " + param2 + " created");
}
}
else
{
//writes to the audit log and to the console when user is added to a new group


//currently the method wont reach here even when I pass four parameters

Console.WriteLine("User " + param3 + " added to group " + param2 + "");
string path3 = "C:\Files\audit.txt"; //doesnt write this to audit.txt
using (StreamWriter sw3 = File.AppendText(path3))
{
sw3.WriteLine("User " + param3 + " added to group " + param2 + "");
}
}
Console.Read();

Respuestas

1 para la respuesta № 1

Mirando la firma del método, la mejor solución sería usar algo similar a esto:

if (String.IsNullOrEmpty(param3)) // you could say that only 3 params were given
{

}
else // you could say that all 4 params were given
{

}

0 para la respuesta № 2

Mirar este tutorial de Microsoft.

Me imagino que su código se vería así:

    static void Main(string[] args)
{
...
if (args.Length == 3) {

//writes to the audit log and to the console when group is made w/out users
Console.WriteLine("Group " + args[1] + " created");
string path2 = "C:\Files\audit.txt";
using (StreamWriter sw2 = File.AppendText(path2))
{
sw2.WriteLine("Group " + args[1] + " created");
}
}
else
{
//writes to the audit log and to the console when user is added to a new group

Como señala el tutorial, también puede usar Environment.CommandLine o Environment.GetCommandLineArgs para acceder a los argumentos de la línea de comandos desde cualquier punto de una consola o aplicación de Windows.