Convert C++ to C#

Asked

Viewed 2,993 times

0

Well, I’m passing some codes I have, and I’m doubtful I can pass that code to c, and I’d like help from anyone who can help, in the following code below

#include <dirent.h>
#include <cstdlib>
#include <iostream>
#include <string.h>
#include <windows.h>

bool extracoes();

int main()
{
    bool saida;
    //"Interface"
    saida = extracoes();
    if(saida)
        std::cout<<"Copias concluidas!!!\n\nPressione qualquer tecla para encerrar!";
    else
        std::cout<<"Erro! Executar instalacao como Suporte\n\nPressione qualquer tecla para encerrar...";
    std::cin.get();
}

bool extracoes()
{
    struct dirent *lsdir;
    DIR *dir;
    int qtd=0, i=0;
    bool padrao;
    std::string path;
    dir = opendir("D:\\Users\\");

    while((lsdir = readdir(dir)) != NULL)
        qtd++;

    std::string users[qtd];
    closedir(dir);

    dir=opendir("D:\\Users\\");
    while((lsdir = readdir(dir)) != NULL)
    {
        padrao=true;
        std::string aux = lsdir->d_name;
        if(aux.size() == 7)
        {
            for(int j=0; j<7; j++)
            {
                if(!(aux[j]<=57 && aux[j]>=48))
                {
                    padrao=false;
                    break;
                }
            }
        }
        else
            padrao=false;

        if(padrao)
        {
            users[i]=aux;
            i++;
        }
    }
    closedir(dir);

    users[i]    = "Default";
    users[i+1]  = "\0";
    i           = 0;
    padrao      = true;

    //Config Global
    if(system("Xcopy kds_kodak c:\\ProgramData\\kds_kodak /v /e /y /h > nul") == 0)
        std::cout<<"Extracao Config_Globais concluida\n\n";
    else
    {
        padrao = false;
        std::cout<<"Erro na extracao Config_Globais\n";
    }
    //Config default e usuários
    while(users[i].compare("\0"))
    {
        std::cout<<"Copiando...\n";
        path = "Xcopy \"Smart Touch\" \"d:\\Users\\";
        path += users[i]+"\\AppData\\Local\\Smart Touch\\\" /v /e /y /h > nul";

        if(system(path.c_str()) == 0)
            std::cout<<"Copia do usuario "<<users[i]<<" concluida!!\n\n";
        else
        {
            std::cout<<"Copia do usuario "<<users[i]<<" nao concluida!!\n\n";
            padrao = false;
        }
        i++;
    }
    return padrao;
    std::cin.get();
}
  • You want an equivalent code in C# or you want a tool that does this conversion for you?

  • if possible an equivalent code, because this application copies a folder of files looking for all the users it has in a user with 7 digits that would be the matricula, but done everything in prompt. But if in c# we could do another method that copies such folder placed in the root of the executable for such a place as it is in the code would also serve

  • 1

    This question seems to be decontextualized because it is a ready code request.

1 answer

2

The ideal would not even be to answer, since a translation of code from one language to another can be done with the use of a tool like this:

https://www.varycode.com/

However, I will put in response some useful directives when converting your code.

1. Create a Console Application

What you have is a console application. Start by creating a project in Visual Studio like Console Application.

2. Entry and exit

cout << "Olá, Mundo";

May be replaced by

Console.WriteLine("Olá, Mundo");

And also:

cin.get();

May be replaced by:

Console.ReadKey();

3. Shell commands

To execute a shell command, use:

System.Diagnostics.Process.Start("CMD.exe", "Xcopy kds_kodak c:\\ProgramData\\kds_kodak /v /e /y /h > nul");

Other codes

Having these baselines, just adapt them to the logic you need.

  • There would be some examples of good programs for conversion, and in case you were wanting to take the same from the application console if possible and pass to windows form

  • I put a link to a free tool. Tangible also has a paid tool. To convert your application to Web Forms, there is no tool. It will actually have to be a reimplementation of your old system.

  • @SNOT, to save the question instead of asking "write code for me", you can make it more generic and suitable to this great response, something like "Guidelines when converting C++ to C#".

Browser other questions tagged

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