C # Unity read folders and subfolders

0

Good morning.

I need my program to read the folders that I have inside a folder, and read the files that I have inside those folders (in this case an .exe and some .png)

The goal is that for each folder that exists, a button with the image inside that folder is created and when clicking the .exe that is inside the folder also .

if (Directory.Exists(rutasapp)) {
            foreach (string dataApp in Directory.GetDirectories(rutasapp)) {
                AppButton button = Instantiate(buttonPrefab);
                button.transform.SetParent(layout);

                //buscamos todos los ficheos que sean .exe y los guardamos en data
                foreach (string data in Directory.GetFiles(@dataApp)) {
                    if (Path.GetExtension(@data) == ".exe") {
                        //al clicar el boton ejecutamos el exe que hemos guardado anteriormente .NO FUNCIONA
                        button.GetComponent<Button>().onClick.AddListener(() => Process.Start(data));
                        UnityEngine.Debug.Log(data);

                    }
                    //buscamos todas las imagenes que sean .png y las guardamos en imagenes
                    foreach (string imagenes in Directory.GetFiles(@dataApp)) {
                        if (Path.GetExtension(@imagenes) == ".png") {
                            //UnityEngine.Debug.Log(imagenes);
                        }
                    }
                }
            }
        }
    }

I have that code that works a lot less well. I create some buttons for each folder I have and then I look for the files that are .exe and .png and I save them in data and images.

When I add this line to run the .exe , when I try to run the program it gives me the following error:

    button.GetComponent<Button>().onClick.AddListener(() => Process.Start(data));

NullReferenceException: Object reference not set to an instance of an object 

Any ideas on how to do it?

    
asked by JuanPerez 05.04.2017 в 21:05
source

0 answers