Khan Laoji
Khan Laoji

Reputation: 19

Collection in c#

I had my data stored in string[] data but I wanted to do the same with a collection. But when I do my recurse calling on my function it delete my old collection because I call it inside it. Can you tell me how to do it right? And how could I use thoses data stored FROM my main class?

EDIT 2 : Now my code is perfect thank you !

/* CLASS DONNEES */

class Donnees
{
    public string NomFichier { get; set; }
    public string Repertoire { get; set; }
    public string Chemin { get; set; }
}

/* THE CALL IN MY MAIN.CS */

 class Program
 {

    static void Main(string[] args)
    {
        var DonneesMain = new List<Donnees>();

       /* ********
       ***MY CODE*
       ******** */
       DonneesMain.AddRange(CreerCollectionDonnees(path, DonneesMain));

        foreach (Donnees Data in DonneesMain)
        {
            System.IO.File.AppendAllText(@"D:\fichier.txt", Data.NomFichier + Environment.NewLine);
            i++;
        }



        Console.WriteLine(i);

/* ///////////MY EVIL METHOD///// */

public static List<Donnees> CreerCollectionDonnees(string path, List<Donnees> M)
    //public static void CreerCollectionDonnees(string path)
    {
        //var lesDonnees = new List<Donnees>();
        List<Donnees> lesDonnees = M;

        try
        {
            if ((File.GetAttributes(path) & FileAttributes.ReparsePoint) != FileAttributes.ReparsePoint)
            {
                //string[] fichiers = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories);
                foreach (string fichier in Directory.GetDirectories(path))
                {
                    //On choisi de ne pas récuperer les données des fichiers commençant par $
                    //if (Path.GetFileName(fichier).Substring(0, 1) != "$")

                        lesDonnees.Add(new Donnees { NomFichier = Path.GetFileName(fichier), Repertoire = Path.GetDirectoryName(fichier), Chemin = Path.GetPathRoot(fichier) });
                        //Console.WriteLine("{0} {1}  ", new string(' ', indent), Path.GetFileName(fichier));
                        CreerCollectionDonnees(fichier, lesDonnees);
                }
            }
        }
        catch (UnauthorizedAccessException) { }

        return lesDonnees;
    }

Upvotes: 1

Views: 93

Answers (3)

Nicola
Nicola

Reputation: 340

the problem in your code is that: when you call the CreerCollectionDonnees , every time program creates a new list by the instruction var lesDonnees = new List<Donnees>();. creating a new empty list, it overwrite the last content of the list! So, you have to declare the lesDonnees list outside the CreerCollectionDonnees method.

hope I've helped you! EDIT: you've declared correctly lesDonnees outside the method, but your mistake is declaring another time lesDonnees inside the method, pay attention:

private static List<Donnees> CreerCollectionDonnees(string path){

   Var lesDonnees = new List<Donnees>();

    try
    {
        if ((File.GetAttributes(path) & FileAttributes.ReparsePoint) != FileAttributes.ReparsePoint)
        {
            //string[] fichiers = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories);
            foreach (string fichier in Directory.GetDirectories(path))
            {
                lesDonnees.Add(new Donnees { NomFichier = Path.GetFileName(fichier), Repertoire = Path.GetDirectoryName(fichier), Chemin = Path.GetPathRoot(fichier) });
                //Console.WriteLine("{0} {1}  ", new string(' ', indent), Path.GetFileName(fichier));
                lesDonnees.AddRange(CreerCollectionDonnees(fichier));
                CreerCollectionDonnees(fichier);
            }
        }
    }
    catch (UnauthorizedAccessException) { }
    return lesDonnees;

everytime it is invoked, it re-declare the lesDonnees list, so it delete previous content! the correct code is follow:

private static List CreerCollectionDonnees(string path) {

    //DO NOT declare lesDonnees!
    try
    {
        if ((File.GetAttributes(path) & FileAttributes.ReparsePoint) != FileAttributes.ReparsePoint)
        {
            //string[] fichiers = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories);
            foreach (string fichier in Directory.GetDirectories(path))
            {
                lesDonnees.Add(new Donnees { NomFichier = Path.GetFileName(fichier), Repertoire = Path.GetDirectoryName(fichier), Chemin = Path.GetPathRoot(fichier) });
                //Console.WriteLine("{0} {1}  ", new string(' ', indent), Path.GetFileName(fichier));
                lesDonnees.AddRange(CreerCollectionDonnees(fichier));
                CreerCollectionDonnees(fichier);
            }
        }
    }
    catch (UnauthorizedAccessException) { }
    return lesDonnees;

}

sorry for the late reply :(

Upvotes: 0

SoftSan
SoftSan

Reputation: 2472

declare list object outside of your method ex. at global level.

   public List<Donnees> lesDonnees = new List<Donnees>();

then you can access it from your Main class.

Update: It's because you're still adding range lesDonnees.AddRange(CreerCollectionDonnees(fichier)); which is actually does not require.

Updated answer based on query.

internal class Program
 {
       private List<Donnees> lesDonnees = new List<Donnees>();
       public static void Main(string[] args)
        {
            Program program = new Program();
            var result  = program.CreerCollectionDonnees(@"C:\YOURPATH");
            // Loop through the collection received in 'result' variable
        }

        private List<Donnees> CreerCollectionDonnees(string path)
        {
            try
            {
                if ((File.GetAttributes(path) & FileAttributes.ReparsePoint) != FileAttributes.ReparsePoint)
                {
                    //string[] fichiers = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories);
                    foreach (string fichier in Directory.GetDirectories(path))
                    {
                        lesDonnees.Add(new Donnees { NomFichier = Path.GetFileName(fichier), Repertoire = Path.GetDirectoryName(fichier), Chemin = Path.GetPathRoot(fichier) });
                        //Console.WriteLine("{0} {1}  ", new string(' ', indent), Path.GetFileName(fichier));
                        CreerCollectionDonnees(fichier);
                    }
                }
            }
            catch (UnauthorizedAccessException) { }
            return lesDonnees;
        }
}

Upvotes: 1

Guffa
Guffa

Reputation: 700152

The collection isn't deleted, you are just creating a new collection for each method call so you don't see the collection created at the level below.

You can return the list from the method, that way you get the list back when you call it, and also the recursive call can get the result from the inner levels:

private static List<Donnees> CreerCollectionDonnees(string path) {
  var lesDonnees = new List<Donnees>();

  if ((File.GetAttributes(path) & FileAttributes.ReparsePoint) != FileAttributes.ReparsePoint) {
    foreach (string fichier in Directory.GetDirectories(path)) {
      lesDonnees.Add(new Donnees { NomFichier = Path.GetFileName(fichier), Repertoire = Path.GetDirectoryName(fichier), Chemin = Path.GetPathRoot(fichier) });    
      lesDonnees.AddRange(CreerCollectionDonnees(fichier));
    }
  }
  return lesDonnees;
}

Upvotes: 0

Related Questions