Bonjour,

J'ai codé un programme qui exporte la liste des utilisateurs d'une OU spécifique vers un fichier .csv.

la connexion à l'AD est fonctionnelle, tout comme l'export des usagers.

Le problème, c'est que c'est très lent (en moyenne, une minute par utilisateur et j'en ai parfois plus de 200).

J'ai testé la même chose en PowerShell, sur le même poste, et ça n'a demandé que quelques secondes.

Alors je me demande si le problème ne vient pas de mon code, le voici :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// ** Export des utilisateurs **
        private void button2_Click(object sender, EventArgs e)
        {
            // Début du sablier
            Cursor.Current = Cursors.WaitCursor;
 
            // S'assurer qu'une OU a bien été saisie
            if (saisieOU == "")
            {
                textBox1.Focus();
                return;
            }
 
            // S'assurer que l'OU recherchée existe
            sourceBase = "OU=Utilisateurs,OU=" + saisieOU + ",OU=Ma Société,OU=INTERNET,DC=FR";
            if (DirectoryEntry.Exists("LDAP://" + sourceBase))
            {
                DE = new DirectoryEntry("LDAP://" + sourceBase);
            }
            else
            {
                // L'OU n'a pas été trouvée
                MessageBox.Show("Impossible de trouver '" + saisieOU + "' dans l'AD.");
                return;
            }
 
            // Recherche des utilisateurs          
            SearchResultCollection results;
            DirectorySearcher DS = null;
            DS = new DirectorySearcher(DE);
            //DS.PropertiesToLoad.Add("Initials");
            DS.PropertiesToLoad.Add("Surname");
            DS.PropertiesToLoad.Add("GivenName");
            DS.PropertiesToLoad.Add("SamAccountName");
            //DS.PropertiesToLoad.Add("Department");
            //DS.PropertiesToLoad.Add("Organization");
            DS.PropertiesToLoad.Add("DisplayName");
 
            // Filtres
            DS.Filter = "(&(objectCategory=person)(objectClass=user))";
            results = DS.FindAll();
 
            // En-tête des colonnes
            DataTable DT = new DataTable();
            //DT.Columns.Add("Initials", typeof(string));
            DT.Columns.Add("Surname", typeof(string));
            DT.Columns.Add("GivenName", typeof(string));
            DT.Columns.Add("SamAccountName", typeof(string));
            //DT.Columns.Add("Department", typeof(string));
            //DT.Columns.Add("Organization", typeof(string));
            DT.Columns.Add("DisplayName", typeof(string));
 
            // Récupération des données
            foreach (SearchResult SR in results)
            {
                DataRow DR = DT.NewRow();
                DirectoryEntry entry = SR.GetDirectoryEntry();
 
                    // Iniatials
                    //if (entry.Properties["Initials"].Count > 0)
                    //{
                    //    DR["Initials"] = entry.Properties["Initials"].Value.ToString();
                    //}
 
                    // Surname
                    if (entry.Properties["sn"].Count > 0)
                    {
                        DR["Surname"] = entry.Properties["sn"].Value.ToString();
                    }
 
                    // GivenName
                    if (entry.Properties["GivenName"].Count > 0)
                    {
                        DR["GivenName"] = Suppression_Caractere_accentue(entry.Properties["GivenName"].Value.ToString());
                    }
 
                    // SamAccountName
                    if (entry.Properties["SamAccountName"].Count > 0)
                    {
                        DR["SamAccountName"] = entry.Properties["SamAccountName"].Value.ToString();
                    }
 
                    // Department
                    //if (entry.Properties["Department"].Count > 0)
                    //{
                    //    DR["Department"] = entry.Properties["Department"].Value.ToString();
                    //}
 
                    // Organization
                    //if (entry.Properties["Organization"].Count > 0)
                    //{
                    //    DR["Organization"] = Suppression_Caractere_accentue(entry.Properties["Organization"].Value.ToString());
                    //}
 
                    // DisplayName
                    if (entry.Properties["DisplayName"].Count > 0)
                    {
                        DR["DisplayName"] = Suppression_Caractere_accentue(entry.Properties["DisplayName"].Value.ToString());
                    }
 
                    // Ajout
                    DT.Rows.Add(DR);
            }
 
            // Export vers le fichier .csv
            filePath = Directory.GetCurrentDirectory() + @"\" + saisieOU + ".csv";
            CreateCSVFile(DT, filePath);
 
            // Fin du sablier
            Cursor.Current = Cursors.Default;
            MessageBox.Show("Export effectué vers " + filePath);
        }
La fonction "Suppression_Caractere_accentue" :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//Le but de cette fonction est de remplacer un ensemble de caractères prédéfinis par d'autres dans un string.
        private string Suppression_Caractere_accentue(string Texte_a_corriger)
        {
            //Conversion du string en tableau de char
            char[] ligne_char = Texte_a_corriger.ToCharArray();
            int mchar;
            for (int i = 0; i < Texte_a_corriger.Length; i++)
            {
                //Conversion du caractere en int
                mchar = (int)ligne_char[i];
                // Remplacement des accents par a
                if (mchar >= 224 && mchar <= 230)
                {
                    ligne_char[i] = 'a'; //(char)97;
                }
                else
                // Remplacement des accents par e
                if (mchar >= 232 && mchar <= 235)
                {
                    ligne_char[i] = 'e'; //(char)101;
                }
                else
                // Remplacement des accents par i
                if (mchar >= 236 && mchar <= 239)
                {
                    ligne_char[i] = 'i'; //(char)105;
                }
                else
                // Remplacement des accents par o
                if (mchar >= 242 && mchar <= 248)
                {
                    ligne_char[i] = 'o'; //(char)111;
                }
                else
                // Remplacement des accents par u
                if (mchar >= 249 && mchar <= 252)
                {
                    ligne_char[i] = 'u'; //(char)117;
                }
                else
                // Remplacement des ç par c
                if (mchar == 231)
                {
                    ligne_char[i] = 'c'; //(char)99;
                }
                else
                // Remplacement des Ð par d
                if (mchar == 208)
                {
                    ligne_char[i] = 'd'; //(char)100;
                }
                else
                // Remplacement des Ñ par n
                if (mchar == 241)
                {
                    ligne_char[i] = 'n'; //(char)110;
                }
                else
                // Remplacement des Ý par y
                if (mchar == 253 || mchar == 255)
                {
                    ligne_char[i] = 'y'; //(char)121;
                }
                else
                // Remplacement des ° par ' '
                if (mchar == 176)
                {
                    ligne_char[i] = ' ';
                }
            }
            // Conversion du tableau de char[] en string
            return new string(ligne_char);
        }
La fonction "CreateCSVFile" :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Fonction de création et d'écriture dans le fichier .csv
        public void CreateCSVFile(DataTable DT, string strPath)
        {
            try
            {
                StreamWriter SW = new StreamWriter(strPath, false);
                int columnCount = DT.Columns.Count;
                for (int i = 0; i < columnCount; i++)
                {
                    SW.Write(DT.Columns[i]);
                    if (i < columnCount - 1)
                    {
                        SW.Write(";");
                    }
                }
                SW.Write(SW.NewLine);
 
                foreach (DataRow dr in DT.Rows)
                {
                    for (int i = 0; i < columnCount; i++)
                    {
                        if (!Convert.IsDBNull(dr[i]))
                        {
                            SW.Write(dr[i].ToString());
                        }
                        if (i < columnCount - 1)
                        {
                            SW.Write(";");
                        }
                    }
                    SW.Write(SW.NewLine);
                }
                SW.Close();
            }
            catch
            {
                throw;
            }
        }
Merci par avance