How to run program in C # by cmd

2

I am creating a program that works with commands, and would like this program to be executed by cmd style:

c:\path\folder>meuprograma
>comando arg1 arg2
>comando 1 falhou
>comando2
>comando 2 executado com sucesso!
>exit
c:\path\folder>

While I'm developing everything works fine, I run my project and it looks like this:

(Janela do console é aberta)
>comando arg1 arg2
>comando 1 falhou
>comando2
>comando 2 executado com sucesso!
>exit
(Janela do console é fechada)

What I want to know is, if I compile my program and register it in Windows variables and I type the above commands will it work? If not, how to make it work.

And how to get the folder where the program was run, for example:

C:\Users\Administrador>cd c:/teste
c:\teste>meuprograma
>help << na execução de um comando 

How to get the folder that the program is running in the case c: / test?

My code:

static void Main(string[] args) {
    do {
        ReadCommand();
        RunCommand();
    } while (command != "exit");
}

static private void RunCommand()
{
    string[] args = command.Split(' ');
    if (args.Length > 0)
    {
        switch (args[0].ToLower())
        {
            case "exit":
                break;
            case "help":
                Help();
                break;
            default:
                InvalidCommand();
                break;
        }
    }

}

static void Help()
{
    string comandos = "\n" +
                      "Diretório atual: " <VARIAVEL_COM_DIRETORIO_AQUI> +
                      "help     - Lista de comandos\n" +
                      "set      - Seta o valor de um parâmetro, possui 2 parâmetros\n" +
                      "           > param       - Nome do parâmetro;\n" +
                      "           > valor       - Valor do parâmetro;\n" +
                      "           Ex: [set repository c:/caminho/do/repositorio]\n"+
                      "get      - Retorna o valor do argumento informado, possui 1 parâmetro\n"+
                      "           > param       - Nome do parâmetro;\n"+
                      "           Ex: [get repository]";

    Console.WriteLine(comandos);
}
    
asked by anonymous 12.05.2015 / 15:07

2 answers

3

Setting the variable Path

For your executable to be found via the command line, you need to include the directory where the program is in the global variable Path .

To see the current contents of the variable, open a comsole and type:

SET PATH

The value will look something like this:

PATH=D:\Program Files\Autodesk\Maya2008\bin;C:\Ruby192\bin;C:\WINDOWS\system32;C :\WINDOWS;C:\WINDOWS\System32\Wbem;C:\PROGRA~1\DISKEE~2\DISKEE~1\;c:\Program Fil es\Microsoft SQL Server\Tools\binn\;C:\Program Files\QuickTime\QTSystem\;D:\P rogram Files\TortoiseSVN\bin;D:\Program Files\Bazaar;C:\Program Files\Android\an droid-sdk\tools;D:\Program Files\Microsoft Visual Studio\Common\Tools\WinNT;D:\P rogram Files\Microsoft Visual Studio\Common\MSDev98\Bin;D:\Program Files\Microso ft Visual Studio\Common\Tools;D:\Program Files\Microsoft Visual Studio\VC98\bin

You can adjust the value of the variable directly in Windows:

System > Advanced System Settings > Tab ' Advanced ' > Environment Variables

Values are separated by semicolons. Add the directory where your application is at the end of string .

Detecting the current directory

Use Environment.CurrentDirectory to return the directory where the program is running.

    
12.05.2015 / 15:50
1

If the ReadCommand() method and the RunCommand method send data to the Console using the Console.Write method and read Console data using the Console.ReadXXX yes method, it will work.

To test, you need to compile the program and run the same program. If the program is run from "cmd", it will use the "cmd" itself to display and read data. If the program is executed by another means, for example by Windows Explorer, it will open a Console window, where there will be user interaction with the program.

To find out the folder the program is running in, I suggest you read this question: #

    
12.05.2015 / 15:50