How to pass four string parameters to c# Console Application that returns a string. This Console App is called from another c# class.

Josh S 26 Reputation points
2024-01-23T06:11:33.68+00:00

Hi I have c# class that call (refer) a c# console application with four string parameters. Console application takes that parameters and returns a string to the calling class. Console application itself has many other classes for various tasks to perform.

Is there any way I can declared those four strings inside the console app and then let the calling class assign correct values to those strings?

I created console app using .NET Framework version 4.7 using VS 2019.

Console app starts the application that do various task once I get all four strings from the calling class. Any other suggestion I can do the above without console application? If so how? Your assistance is greatly appreciated.

Thank you

 
Developer technologies C#
{count} votes

2 answers

Sort by: Most helpful
  1. Olaf Helper 47,436 Reputation points
    2024-01-23T06:52:01.3366667+00:00

    Your post is not very clear and the code is missing. You can use commandline arguments to pass parameter to a console app, see Main() and command-line arguments

    1 person found this answer helpful.

  2. Anonymous
    2024-01-23T07:10:02.2533333+00:00

    Hi @Josh S , Welcome to Microsoft Q&A,

    In addition to the command line, you can also use the configuration file method.

    Add a configuration file in the console application: You can add a configuration file (for example, app.config or .config file) in the console application and store these four string parameters in the configuration file.

    <configuration>
         <appSettings>
             <add key="Param1" value="DefaultValue1" />
             <add key="Param2" value="DefaultValue2" />
             <add key="Param3" value="DefaultValue3" />
             <add key="Param4" value="DefaultValue4" />
         </appSettings>
    </configuration>
    

    Reading configuration file in console application: In console application, you can use ConfigurationManager class to read the values from the configuration file.

    using System.Configuration;
    
    class Program
    {
         static void Main()
         {
             string param1 = ConfigurationManager.AppSettings["Param1"];
             string param2 = ConfigurationManager.AppSettings["Param2"];
             string param3 = ConfigurationManager.AppSettings["Param3"];
             string param4 = ConfigurationManager.AppSettings["Param4"];
    
             //Execute the task using param1, param2, param3 and param4
         }
    }
    

    Modify the configuration file from the calling class: In the calling class, you can assign values to these four parameters by modifying the configuration file. Note that this may require write permissions to the configuration file.

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.