How to run program in C# by cmd

Asked

Viewed 4,336 times

2

I am creating a program that works with commands, and I 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 is working as per, I run my project and it appears to me 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 the Windows variables and I type the above commands it will work? If not, how to make it work.

And how to take the folder in which the program was executed, for example:

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

How to get the folder the program is running in 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);
}
  • If the method ReadCommand and the RunCommand send strings to the Console.Write and read strings by Console.ReadXXX yes, it will work. Just test, compile the program, go to its folder by cmd and executes him.

  • :O é verdade, não manjo muito de C#, I’m learning now... Anyway, I have another question, I will supplement the question.

  • @Marciano.Andrade, if you can answer this other question. :)

2 answers

3


Adjusting the variable Path

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

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

SET PATH

The value will be something similar to the following:

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\90\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 variable value directly on Windows:

System > Advanced System Settings > Flap 'Advanced' > Environment Variables

inserir a descrição da imagem aqui

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

Detecting the current directory

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

1

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

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

To find out the folder in which the program is running, I suggest you read this question: C# - Path to execution

  • Marciano, thank you for the answer, but this answer from the Path to execution was not very clear to me that I am a beginner in C# and know nothing about Assembly. The reply of @Onosendai became much clearer to me and worked perfectly.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.