MODIFICATION AND READING OF DIRECTORIES IN C#

Asked

Viewed 70 times

-3

I am developing a C#project, which uses python scripts as an engine (I did not use Ironpython due to its limitations, I work with pandas), what I do is simply run the script through a PROCESS in c#, passing which python.exe directory and which script directory 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 cannot leave these directories, both python and scripts as something fixed on the system, so they will give problem on other machines...

Could you tell me how to configure the directories for each installation in a non-manual way? I thought of using a txt to store the directories and read straight from it... it is easy to edit a txt and would end the problem, but I’m having difficulties with it too. I need to know the txt directory so I can read and edit it...

From now on, thank you!

2 answers

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 variables commented 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);
        }
    }

0

A viable solution would be to read the PATH of the Python installation.

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

The installation path would be in the environment variables.

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

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

Browser other questions tagged

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