This question has already been answered:

Tell me, please: for example

Starting the application -> Specify the string value -> Application operation -> Get the string value

Those. There is a goal of creating a certain module that could be used by different software with some input data and depending on this result. For example, they gave the application a link to the site, got the result, if the specified text is there - got true / false .

Questions:

  • Can this be done in the console application?
  • If so, how to accept and how to send values?
  • another variant?

Reported as a duplicate by members Grundy , Denis , iluxa1810 , αλεχολυτ , Bald 18 Nov '16 at 3:27 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

3 answers 3

Other replies have already written about the command line parameters and the return code.

But, as it seems to me, standard input-output streams and their redirection are more suitable for you.

For example, let's write such a simple console program:

 using System; class Program { static void Main() { // Читаем из стандартного потока ввода string line = Console.ReadLine(); // Обрабатываем введенное значение line = line.ToUpper(); // Пишем в стандартный поток вывода Console.WriteLine(line); } } 

By the way, you can explicitly write Console.In.ReadLine() and Console.Out.WriteLine() , indicating input and output streams.

If you run it, you will need to enter a string in the console. The result will also appear in the console window.

Now you can use redirection streams. For example, let us input a text file from our program, from which it will read the value (one first line). The output is redirected to another text file.

 app < in.txt > out.txt 

Where app.exe is the name of our program. in.txt is the input file. out.txt is the output file.

You read about the pipeline and the flow of errors yourself.

    I answer the question how I understood it :)

    Probably, you want your console application to start with certain parameters ( ping localhost , where localhost is a parameter)

    Create a console project, look at the header of the main method:

     static void Main(string[] args) 

    The args array is the input parameters.

    If you do this:

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

    And then compile, and in the command line type ConsoleApplication1 print hello - hello will be displayed in the console

    Next yourself :)

      In the answer above you were given a code, even two times (I will not repeat it), how to take command line parameters and process them in a console application. The bottom line is that you call your application, and in the call string you pass them to the application through the string array args .

      After you have received the necessary parameters, in your example the name of the site, your application performs the necessary actions and terminates. Upon completion, you can specify an integer ( int ) return code, also known as ExitCode , which can be processed in the calling application. Those. true or false as you want, return does not work, but 0 or 1 (or some other type of int ) - easily.

      You can return by overriding the Main() method in a console application, replacing void with an int , like this:

       static int Main(string[] args) { int i = 0; Console.WriteLine("Hello! I'll try to write command-line arguments."); foreach (var parameter in args) { Console.WriteLine("Parameter " + i.ToString() + " is: "+args[i]); } Console.WriteLine("Press ENTER to exit"); Console.ReadLine(); return args.Count(); } 

      In this code, the program can receive some command line arguments as input, and tries to display them on the console screen, then terminates with a return code equal to the number of arguments.

      In addition, you can get the same result (application termination with a return code) using the Environment.Exit(code) ( MSDN ) method, which allows you to complete the work not only at the end of the Main() method, but also from another place in the program. You can try instead of return args.Count(); write like this: Environment.Exit(args.Count()); (just back void instead of int ).

      A slightly modified and more flexible option for solving your task will use some kind of buffer (or buffers) for data exchange, for example, files: we run the program and as a command line argument you give it the file name with some input data, and the name of the file where the result will be displayed. The program processes the input data, with their account it performs some actions, and the result is written in the desired form into the output file, which can then be processed as you like.

      There are various tools for working with files in the C # arsenal, and talking about all the possibilities of how to write / read a file is too long and too much, but you can read about it, for example, here , or here , or here . Or google and find more sources)