1

I need to create a console application that can take multiple different launch arguments (which can be added using the batch file). I so far tried this, but it seems like I don't understand it correctly.

Code:

        static void Main(string[] args)
    {
        ProcessStartInfo processStartInfo = new ProcessStartInfo();
        processStartInfo.Arguments = LaunchArguments.Operation_AddLocale;
        processStartInfo.Arguments = LaunchArguments.Operation_CreateTextFile;
        processStartInfo.UseShellExecute = false;
        if (processStartInfo.Arguments == LaunchArguments.Operation_CreateTextFile)
        {
            Console.WriteLine("Creating a text file.");
            File.Create("file.txt");
            Console.Write("Done!");
        }

        if (processStartInfo.Arguments == LaunchArguments.Operation_AddLocale)
        {
            if (Directory.Exists("locale"))
            {
                    try
                    {
                        File.Create("locale.txt");

                        
                    }
                    catch (Exception ex)
                    {
                    Console.WriteLine("An error occured: ");
                    Console.Write(ex.ToString());
                    Console.Write(ex.Message.ToString());
                    Console.ReadLine();
                    }

            }
        else
        {
            Console.WriteLine("Incorrect Input, quitting");
            Console.WriteLine("This application only accepts arguments of type '-CreateTextFile; -AddLocale'");
            Console.Beep();
            Console.ReadLine();
        }
    }

I made a constant string 'Operation_AddLocale' and 'Operation_CreateTextFile'.

by batch file:

@echo off
start SW_project_generator.exe -AddLocale

This all should do that if I launch the application via this batch file, it will do the operations that are in the 'if (processStartInfo.Arguments == LaunchArguments.Operation_AddLocale)' and if the batch file would add the '-CreateTextFile' argument, it will go to the 'if (processStartInfo.Arguments == LaunchArguments.Operation_CreateTextFile)'.

However, when I launch this app via my batch file, it will always just use the first argument (which is the '-CreateTextFile') and creates a text file and then goes to the else option.

Application's Output:

Creating a text file. Done!Incorrect Input, quitting This application only accepts arguments of type '-CreateTextFile; -AddLocale;'

Alright so, my question is, how to make this working, that if I create a batch file give it a argument of type '-AddLocale' it will just go to the 'AddLocale' operations and if I give it something else that is defined in the app, like the 'CreateTextFile', it'll go to it's 'if' statement. And finnaly, if the launch argument will be equal to nothing or wrong one, it'll show the quitting message.

Thanks everyone for help.

  • 2
    Your program does not read the commandline arguments (```string[] args```). In stead, you are setting the commandline arguments yourself, and the ```processStartInfo.Arguments``` that you set to ```LaunchArguments.Operation_AddLocale``` is one line later replaced by the line ```processStartInfo.Arguments = LaunchArguments.Operation_CreateTextFile;```. That is why the program gives you this output – suitendaal Aug 15 '22 at 09:08
  • Well yeah, but I'm wondering how to make this work – Technika číslo 1 s.r.o Aug 15 '22 at 09:19
  • There is an entire [article](https://stackoverflow.com/questions/491595/best-way-to-parse-command-line-arguments-in-c) on how to parse commandline arguments in C#. However, in your case you could just check whether the ```args``` contains the strings ```LaunchArguments.Operation_CreateTextFile``` and ```LaunchArguments.Operation_AddLocale``` – suitendaal Aug 15 '22 at 09:28
  • You really should learn to better format your code, but more importantly, you have not posted a [mcve], because your opening block does not have a closing brace. – Compo Aug 15 '22 at 10:54
  • what do you mean by the 'if args contains the strings?' – Technika číslo 1 s.r.o Aug 15 '22 at 13:23

0 Answers0