CHANGING AND READING DIRECTORIES IN C #

-3

I'm developing a C # project, which uses python scripts as the engine (I did not use IronPython because of its limitations, I work with pandas), what I do is simply execute the script through a PROCESS in C #, passing the python.exe directory and which directory of the script I want to run, EXAMPLE:

    private void ExecutarScript()
    {
        this.bnt_ExecutarScript.Enabled = false;

        string p_cmd = @"C:\Users\dalton.takeuchi\Anaconda3\python.exe";
        string file = @"C:\PROJETO 11\Python\VMN\VMN.py";

        run_script(p_cmd, file);

        this.CarregarGridSimulacao();
        this.bnt_ExecutarScript.Enabled = true;
    }

    private void run_script(string p_cmd, string file)
    {
        if (processoEmExecucao == false)
        {
            try
            {
                processoEmExecucao = true;
                p_cmd = string.Format(@"'{0}'", p_cmd).Replace("'", "\"");
                file = string.Format(@"'{0}'", file).Replace("'", "\"");

                ProcessStartInfo start = new ProcessStartInfo(p_cmd, file);
                start.UseShellExecute = false;
                start.RedirectStandardOutput = true;
                start.CreateNoWindow = true;

                using (Process process = Process.Start(start))
                {
                    process.WaitForExit();

                    /*
                    using (StreamReader reader = process.StandardOutput)
                    {
                        string result = reader.ReadToEnd();
                        //MessageBox.Show(result," PYTHON]:",MessageBoxButtons.OK,MessageBoxIcon.Information);
                    }
                    */
                }
            }

            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
            finally
            {
                processoEmExecucao = false;

            }
        }
    }

The problem is that I can not leave these directories, both python and scripts as something fixed on the system, so that they will give trouble on other machines ...

Could you give me a way to configure the directories for each installation in a way that is not manual? I thought of using a txt to store the directories and read it straight ... it's easy to edit a txt and it would end the problem, but I'm having difficulty with that as well. I need to know the txt directory so you can read and edit it ...

Thanks in advance!

    
asked by anonymous 13.09.2018 / 17:44

2 answers

0

A workable solution would be to read the PATH from the Python installation.

Your program would have a prerequisite that would be python installed, and from that you would be able to read the python installation path.

The installation path would be in the environment variables.

So instead of requiring you to add the path in a txt, you would simply read the installation path.

If this is not possible, you just need to read the installation directory manually, in which case the default python installation path would be static.

    
13.09.2018 / 18:17
0

Take a look at this code.

Basically do what you suggested: Create a configuration file and, if it exists, just read it and the values are returned in the commented variables below.

    private void GetPath()
    {
        // Diretório do programa (C#)
        string appPath = AppDomain.CurrentDomain.BaseDirectory;

        if (File.Exists(appPath + @"\config.ini"))
        {
            //                          Referência: System.IO
            string[] getConfigContent = File.ReadAllLines(appPath + @"\config.ini");

            // Seu código...
            // getConfigContent[0] = Primeiro diretório
            // getConfigContent[1] = Segundo diretório
        }
        else
        {
            string[] setConfigContent = new string[2];
            setConfigContent[0] = "Path1"; // Primeiro diretório
            setConfigContent[1] = "Path2"; // Segundo diretório

            File.WriteAllLines(appPath + @"\config.ini", setConfigContent);
        }
    }
    
14.09.2018 / 16:21