leojg
leojg

Reputation: 1186

Run a C# class in command line

I have a C# class that Encrypt/Decrypt strings.

Is there any posibilite to run it in the command line?

For example:

cryptography encrypt "helo world" >> file.txt

I am not very experienced in C# so maybe the solution is quite simple and I do not realize it

Upvotes: 0

Views: 274

Answers (2)

user1519979
user1519979

Reputation: 1874

create a console application and check the command line arguments

e.g.:

static void Main(string[] args){
    if (args.Length == 2)
    {
        if (args[0] == "encrypt")
        {
            Console.WriteLine(encrypt(args[1]));
        } else if(args[0] == "decrypt"){
            Console.WriteLine(decrypt(args[1]));
        }
    }
}

Upvotes: -1

Marc Gravell
Marc Gravell

Reputation: 1063328

That is basically a "console" application; create a project (or edit your existing project) so that the output type is "Console Application". Then add a Main entry-point:

public static void Main(string[] args) {
   // TODO: your code here
}

The args contains the parameters in order; "encrypt", "hello world". Write your output to stdout via Console.Out typically via Console.Write / Console.WriteLine - or if you need binary output, Console.OpenStandardOutput() - and it will automatically work with pipes such as >>. For extra credit, change the return type of Main to int to return the errorlevel of the exe.

Upvotes: 3

Related Questions