IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C# Discussion :

Besoin du système de projet de SharpDevelop


Sujet :

C#

  1. #1
    Membre averti Avatar de M.Leroy
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Février 2019
    Messages
    49
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Février 2019
    Messages : 49
    Par défaut Besoin du système de projet de SharpDevelop
    Bonjour à toutes et tous,
    Je vais essayer de faire simple. Je bosse sur la création d'un environnement de développement open source pour un langage peu connu. Il me manque le système de solution et projet avec l'explorateur de solutions :
    Nom : Explorateur de solution.PNG
Affichages : 506
Taille : 27,5 Ko

    si possible il serait pratique d'avoir un explorateur de fichier intégré
    Nom : Explorateur de fichier.PNG
Affichages : 506
Taille : 59,1 Ko

    je souhaite créer mon type de projet .osmproj (extension perso même si c'est du .xml en réalité). Dans un projet .osmproj il y aurait des fichiers .cpc (.texte) qui sont les fichiers dans le langage peu connu (CC+) et des fichiers .xml qui ouvrent le concepteur de fenêtre.
    Je suis donc intéressé par la source du logiciel SharpDevelop https://github.com/icsharpcode/SharpDevelop
    Si je sollicite votre aide c'est que je n'ai pas les mêmes ambitions que SharpDevelop qui comprend 78 projets dans sa solution , je voudrais simplement en extraire le système de projet (image 1) ou/et le système d'exploration de fichier (image 2) avec le tree view qui permet de naviguer, créer des dossiers..., je me perd dans la solution de SharpDevelop c'est pourquoi après plusieurs mois de galère j'aimerai que quelqu'un essay pour moi de créer un projet c# avec la tree view en question (explorateur de solution/fichier) en essayant d'isoler le systéme de projet de SharpDevelop dans seul petit projet c# capable d'ouvrir des fichiers texte dans des onglets d'un tab page par exemple.

    N'hésitez pas à poser des questions.

    Je vous remercie !

  2. #2
    Membre Expert

    Homme Profil pro
    Développeur .NET
    Inscrit en
    Novembre 2010
    Messages
    2 067
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Novembre 2010
    Messages : 2 067
    Par défaut
    Pourquoi ne pas redévelopper ces composants toi même ?
    https://docs.microsoft.com/fr-fr/dot...w-and-treeview

    Sinon je me demande s'il serait pas plus simple de créer un plugin pour visual studio code que de créer ton propre Ide.

  3. #3
    Membre averti Avatar de M.Leroy
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Février 2019
    Messages
    49
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Février 2019
    Messages : 49
    Par défaut
    Je connais déjà ce tuto microsoft qui ne m'a pas franchement aidé.
    Pourquoi pas un plugin visual studio code car j'ai besoin de concepteur de fenêtre et que on préfère dépendre de notre propre IDE pour cela aussi que la piste de l'extension visual studio community n'est pas privilégiée et quand elle l'a été j'ai trouvé ça hyper galère surtout que le fichier de coloration syntaxique ne fonctionnait pas etc.
    J'ai déjà bien avancé dans mon projet depuis 6 ans et il reste que le système de projet à finaliser qui ne relève pas non plus de la sorcellerie mais qui est mal documenté sur le web , obligé alors d'essayer de comprendre par soi-même les sources d'IDE en C#. Au plus simple je voudrais simplement un explorateur de fichier Image 2 du précédent post dans la tree view , pas forcement de système de projet.
    Nom : OSM33.PNG
Affichages : 453
Taille : 52,5 Ko
    (Donc manque le système de projet ou plus simple l'explorateur de fichier dans la tree view en haut à droite).
    Merci

  4. #4
    Membre averti Avatar de M.Leroy
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Février 2019
    Messages
    49
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Février 2019
    Messages : 49
    Par défaut
    Le tuto m'a finalement été utile voici ce que j'ai réussi à faire
    Nom : explorer perso.PNG
Affichages : 430
Taille : 22,2 Ko

  5. #5
    Membre Expert
    Avatar de wallace1
    Homme Profil pro
    Administrateur systèmes
    Inscrit en
    Octobre 2008
    Messages
    1 966
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Puy de Dôme (Auvergne)

    Informations professionnelles :
    Activité : Administrateur systèmes
    Secteur : Aéronautique - Marine - Espace - Armement

    Informations forums :
    Inscription : Octobre 2008
    Messages : 1 966
    Billets dans le blog
    7
    Par défaut
    @M.Leroy : bien joué, il ne te reste plus qu'à intégrer ton imageList avec ta collection d'icônes d'arborescence et fichiers.

  6. #6
    Membre averti Avatar de M.Leroy
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Février 2019
    Messages
    49
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Février 2019
    Messages : 49
    Par défaut
    Oui d'ailleurs je me demande pourquoi l'imagelist fonctionne pour la listview et pas pour la treeview. Par contre pour ce qui est d'ajouter/supprimer/copier/coller/renommer des dossiers/fichiers avec un contextmenu je trouve pas de source ou tutoriel

  7. #7
    Membre Expert

    Homme Profil pro
    Développeur .NET
    Inscrit en
    Novembre 2010
    Messages
    2 067
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Novembre 2010
    Messages : 2 067
    Par défaut
    C'est fait en windows forms ton ide ?

  8. #8
    Membre averti Avatar de M.Leroy
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Février 2019
    Messages
    49
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Février 2019
    Messages : 49
    Par défaut
    Oui ce n'est pas du Wpf

  9. #9
    Membre extrêmement actif
    Inscrit en
    Avril 2008
    Messages
    2 573
    Détails du profil
    Informations personnelles :
    Âge : 65

    Informations forums :
    Inscription : Avril 2008
    Messages : 2 573
    Par défaut
    Citation Envoyé par M.Leroy Voir le message
    Oui d'ailleurs je me demande pourquoi l'imagelist fonctionne pour la listview et pas pour la treeview. Par contre pour ce qui est d'ajouter/supprimer/copier/coller/renommer des dossiers/fichiers avec un contextmenu je trouve pas de source ou tutoriel
    Pour créer des "folders",des "files",ou bien ouvrir un "file" pour édition dans un TextBox ou (RichTextBox) le Treeview le fait facilement a condition de "jongler" avec Directory Info
    voici un exemple code :
    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
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
     
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
     
    namespace WindowsFormsApplication1
    {
        public partial class FormExplorer : Form
        {
            private string path = WindowsFormsApplication1.Properties.Resources.MyAppPath;
     
            public FormExplorer()
            {
                InitializeComponent();
     
     
            }
     
     
     
     
            private void FormExplorer_Load(object sender, EventArgs e)
            {
                this.tv.ContextMenu = this.contextMenu1;
                // Clear All Nodes if Already Exists  
                this.tv.Nodes.Clear();
     
                if (path != "" && Directory.Exists(path))
                    LoadDirectory(path);
     
     
     
            }
            private void LoadDirectory(string dir)
            {
                DirectoryInfo di   = new DirectoryInfo(dir);
     
                TreeNode newNode = this.tv.Nodes.Add(di.Name);
                newNode.Tag = di.FullName;
                newNode.StateImageIndex = 0;
                LoadFiles(dir, newNode);
                LoadSubDirectories(dir, newNode);
     
            }
            private void LoadSubDirectories (string dir,TreeNode node)
            {
                string[] subdirectoryEntries  = Directory.GetDirectories(dir);
                // Loop through them to see if they have any other subdirectories  
                foreach(string  subdirectory  in subdirectoryEntries)
                {
                    DirectoryInfo di= new DirectoryInfo(subdirectory);
                    TreeNode newNnode   = node.Nodes.Add(di.Name);
                    newNnode.StateImageIndex = 0;
                    newNnode.Tag = di.FullName;
                    LoadFiles(subdirectory, newNnode);
                    LoadSubDirectories(subdirectory, newNnode);
     
     
                 }
            }  
            private void LoadFiles(string dir, TreeNode td)
            {
                string[] files  = Directory.GetFiles(dir, "*.txt");
     
                // Loop through them to see files  
                foreach (string file in files)
                {   
                    FileInfo fi   = new FileInfo(file);
                    TreeNode tnode   = td.Nodes.Add(fi.Name);
                    tnode.Tag = fi.FullName;
                    tnode.StateImageIndex = 1;
                }
     
            }
     
            private void menuNewItem_Click(object sender, EventArgs e)
            {
                if (this.tv.SelectedNode == null) return;
     
                string pathDir=(string)this.tv.SelectedNode.Tag;
                DirectoryInfo di = new DirectoryInfo(pathDir);
     
                // add file to  di
                string newFileName = GetNewFileName(di);
     
                // Adds new node as a child node of the currently selected node.
                TreeNode newNode = new TreeNode(newFileName);
                newNode.Tag =di.FullName + @"\\" + newFileName ;
                newNode.ImageKey="NewFile";
                this.tv.SelectedNode.Nodes.Add(newNode);
     
            }
     
            private void menuNewFolder_Click(object sender, EventArgs e)
            {
                if (this.tv.SelectedNode == null) return;
     
                TreeNode selNode = this.tv.SelectedNode;
     
                string pathDir = (string)selNode.Tag;
                DirectoryInfo di = new DirectoryInfo(pathDir);
     
                // add subdirectory to  di
                string newDirectoryName = GetNewFolderName(di);
     
                // Adds new node as a child node of the currently selected node.
                TreeNode newNode = new TreeNode(newDirectoryName);
                newNode.Tag = di.FullName + @"\\" + newDirectoryName;
                newNode.ImageKey = "NewFolder";
     
                selNode.Nodes.Add(newNode);
            }
     
            private void menuOpenFile_Click(object sender, EventArgs e)
            {
                if (this.tv.SelectedNode == null) return;
     
                TreeNode selNode = this.tv.SelectedNode;
     
                if (selNode.Text.EndsWith("txt"))
                {
                    string pathDir = (string)selNode.Tag;
                    FileInfo fi = new FileInfo(pathDir);
     
                    // edit file
                    using (StreamReader sr = new StreamReader(fi.FullName)) 
                    {
                        BoxEditor.Clear();
                        BoxEditor.Text = sr.ReadToEnd();
     
                        tabControl1.SelectedIndex = 1;
                        tabPage2.Show();
                    }
                }
     
     
            }
            private void menuDelete_Click(object sender, EventArgs e)
            {
                if (this.tv.SelectedNode == null) return;
     
                TreeNode selNode = this.tv.SelectedNode;
     
                string pathDir = (string)selNode.Tag;
                if (pathDir.Contains("txt") || pathDir.Contains("xml"))
                {
                    FileInfo fi = new FileInfo(pathDir);
                    fi.Delete();
                }
                else 
                {
                    DirectoryInfo di = new DirectoryInfo(pathDir);
                    di.Delete(true);
                }
     
                // Removes currently selected node
                this.tv.Nodes.Remove(selNode);
     
            }
            private string GetNewFileName(DirectoryInfo di)
            {
                string newItem = "NewItem";
                string[] files = Directory.GetFiles(di.FullName , "*.txt");
                string fileName =di.FullName+ @"\"+newItem;
                int countFile = 0;
                if (files.Length == 0)
                    countFile = 1;
                else
                    countFile = files.Length + 1;
     
                fileName += countFile.ToString() + ".txt";
     
                FileInfo fi = new FileInfo(fileName);
     
                //Create a file .
                using (StreamWriter sw = fi.CreateText()) 
                {
                    sw.WriteLine(newItem);
                    sw.WriteLine("Hello");
                    sw.WriteLine("And");
                    sw.WriteLine("Welcome");
                }
                newItem += countFile.ToString() + ".txt";
                return newItem;
     
            }
            private string GetNewFolderName(DirectoryInfo di)
            {
                string newFolder = "NewFolder";
                string[] subdirs = Directory.GetDirectories(di.FullName, newFolder+"*");
                string dirName = di.FullName + @"\\" + newFolder;
                int countDir = 0;
                if (subdirs.Length == 0) 
                    countDir = 1;
                else
                    countDir = subdirs.Length + 1;
                  
     
                dirName += countDir.ToString();
                DirectoryInfo newDir = new DirectoryInfo(dirName);
     
                //Create a  directory
                newDir.Create();
     
                newFolder += countDir.ToString();
                return newFolder;
     
            }
     
     
     
     
     
        }
    }
    Tu observeras qu'il "faut suivre" le mappage entre la prop text du TV et le Directory.FullName ou le path du FileInFileInfo .

    Si tu veux renommer un "folder" ou un "file" tu dois renommer :
    -d'ABORD le Directory.FullName ou le path du FileInFileInfo ensuite mettre à jour la prop Text du TV.

    Relativement à ta question concernant un "basic project system" (comme disent les péquenots de Microsoft dans le lien ci-dessous) avec une "custom extension" cet article MS le décrit ,même si je le trouve un peu nébuleux( "seem confusing" ).
    Il entre dans le cadre de l'extensibilty.
    Bonne lecture.

    https://docs.microsoft.com/en-us/vis...-system-part-1


    bon code.

  10. #10
    Membre extrêmement actif
    Inscrit en
    Avril 2008
    Messages
    2 573
    Détails du profil
    Informations personnelles :
    Âge : 65

    Informations forums :
    Inscription : Avril 2008
    Messages : 2 573
    Par défaut
    rebonjour

    OUPS ! dans le code poste du TV j'ai oublie de mentionner qu'il faut ajouter un "node" racine ou root au TV dit prebuilt-node en MODE DESIGN.
    J' assigne à ce node ce qui suit:
    - prop name =ExplorerNode.
    - prop text= ExplorerProject.

    bon code...

  11. #11
    Membre averti Avatar de M.Leroy
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Février 2019
    Messages
    49
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Février 2019
    Messages : 49
    Par défaut
    Merci Mabrouki ton code fonctionne bien
    Pour renommer un dossier ou fichier je me suis inspiré d'une source VB traduite en c#
    http://www.vbforums.com/showthread.p...AfterLabelEdit
    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
     
    private void trvLocal_AfterLabelEdit(object sender, System.Windows.Forms.NodeLabelEditEventArgs e)
    {
        WindowsFormsApplication1 info = (WindowsFormsApplication1)e.Node.Tag;
        string dir = System.IO.Path.GetDirectoryName(info.FullPath);
        string txt = e.Label;
     
     
     
        if (IsValidFileOrFolderName(txt))
        {
            if (txt == null)
                MessageBox.Show("Aucun changement");
            else
            {
                string newName = System.IO.Path.Combine(dir, txt);
     
     
     
     
                // Now you determine what type of node this is
     
                switch (info.Type)
                {
                    case 0 // It's a drive... Cancel the edit
                   :
                        {
                            e.CancelEdit = true;
                            break;
                        }
     
                    case 1 // it's a folder
             :
                        {
                            System.IO.Directory.Move(info.FullPath, newName);
                            break;
                        }
     
                    case 2 // it's a file
             :
                        {
                            System.IO.File.Move(info.FullPath, newName);
                            break;
                        }
                }
     
                // And finally you update the node info 
                info.FullPath = newName;
            }
        }
        else
            e.CancelEdit = true;
    }
    Maintenant je vais voir comment résoudre le problème de l’exception lorsque on veut créer un dossier à partir d'un noeud fichier et non d'un noeud dossier.
    Effectivement on peut pas créer un dossier dans un fichier
    Et comment personnaliser les icônes de la tree view en fonction de si c'est un noeud dossier ou un noeud de fichier .txt .xml histoire d'avoir des icônes persos.
    et pourquoi pas après la faculté de copier coller.
    Merci si vous avez des idées de code en parallèle de mes recherches.

  12. #12
    Membre averti Avatar de M.Leroy
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Février 2019
    Messages
    49
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Février 2019
    Messages : 49
    Par défaut
    Bon finalement le système after label edit ne fonctionne pas il veut pas effectuer un cast d'un objet de type System.string en type WindowsFormsApplication1 donc impossible de renommer avec mon code

  13. #13
    Membre extrêmement actif
    Inscrit en
    Avril 2008
    Messages
    2 573
    Détails du profil
    Informations personnelles :
    Âge : 65

    Informations forums :
    Inscription : Avril 2008
    Messages : 2 573
    Par défaut
    Citation Envoyé par M.Leroy Voir le message
    Bon finalement le système after label edit ne fonctionne pas il veut pas effectuer un cast d'un objet de type System.string en type WindowsFormsApplication1 donc impossible de renommer avec mon code
    Ce code est ou incomplet ou pipé.
    Extrait de la MSDN Lib fr intitulé "TreeNode.IsEditing, propriété " avec un exemple code .
    L'exemple de code suivant permet à l'utilisateur de modifier des nœuds d'arbre non racine en utilisant ContextMenu. Quand l'utilisateur clique avec le bouton droit, TreeNode à cette position est déterminé et stocké dans une variable appelée mySelectedNode. Si un nœud d'arbre non racine est sélectionné, celui-ci passe dans l'état modifiable, ce qui permet à l'utilisateur de modifier l'étiquette du nœud. Une fois que l'utilisateur a fini de modifier l'étiquette du nœud d'arbre, le nouveau texte d'étiquette est évalué et enregistré. Dans cet exemple, plusieurs caractères sont considérés non valides dans le texte d'étiquette. Si l'un des caractères non valides est présent dans la chaîne d'étiquette, ou la chaîne est vide, l'erreur est notifiée à l'utilisateur et le texte précédent de l'étiquette est retourné.



    code behind.cs accompagnant cette rubrique texto:
    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
     
    /* Get the tree node under the mouse pointer and 
       save it in the mySelectedNode variable. */
    private void treeView1_MouseDown(object sender, 
      System.Windows.Forms.MouseEventArgs e)
    {
       mySelectedNode = treeView1.GetNodeAt(e.X, e.Y);
    }
     
    private void menuItem1_Click(object sender, System.EventArgs e)
    {
       if (mySelectedNode != null && mySelectedNode.Parent != null)
       {
          treeView1.SelectedNode = mySelectedNode;
          treeView1.LabelEdit = true;
          if(!mySelectedNode.IsEditing)
          {
             mySelectedNode.BeginEdit();
          }
       }
       else
       {
          MessageBox.Show("No tree node selected or selected node is a root node.\n" + 
             "Editing of root nodes is not allowed.", "Invalid selection");
       }
    }
     
    private void treeView1_AfterLabelEdit(object sender, 
             System.Windows.Forms.NodeLabelEditEventArgs e)
    {
       if (e.Label != null)
       {
         if(e.Label.Length > 0)
         {
            if (e.Label.IndexOfAny(new char[]{'@', '.', ',', '!'}) == -1)
            {
               // Stop editing without canceling the label change.
               e.Node.EndEdit(false);
            }
            else
            {
               /* Cancel the label edit action, inform the user, and 
                  place the node in edit mode again. */
               e.CancelEdit = true;
               MessageBox.Show("Invalid tree node label.\n" + 
                  "The invalid characters are: '@','.', ',', '!'", 
                  "Node Label Edit");
               e.Node.BeginEdit();
            }
         }
         else
         {
            /* Cancel the label edit action, inform the user, and 
               place the node in edit mode again. */
            e.CancelEdit = true;
            MessageBox.Show("Invalid tree node label.\nThe label cannot be blank", 
               "Node Label Edit");
            e.Node.BeginEdit();
         }
         this.treeView1.LabelEdit = false;
       }
    }

    Je reposte (reposter n'existant pas le correcteur m’affiche riposte) le code déjà communique complété par le code MSDN ci-dessus avec un menuItemRename
    pour renommer un DirInfo ou un FileInfo.
    Il utilise DirInfo.Move(string destination) ou le paramètre "destination" est un "full name" ou chemin d’accès complet qualifie.
    Le folder ou fichier selon le cas doit être supprimée évidemment après l’opération Move.
    code revu :
    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
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
     
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
     
    namespace WindowsFormsApplication1
    {
        public partial class FormExplorer : Form
        {
            private string path = WindowsFormsApplication1.Properties.Resources.MyAppPath;
     
            public FormExplorer()
            {
                InitializeComponent();
     
     
            }
     
     
     
     
            private void FormExplorer_Load(object sender, EventArgs e)
            {
                this.tv.ContextMenu = this.contextMenu1;
                // Clear All Nodes if Already Exists  
                this.tv.Nodes.Clear();
     
                if (path != "" && Directory.Exists(path))
                    LoadDirectory(path);
     
     
     
            }
            private void LoadDirectory(string dir)
            {
                DirectoryInfo di = new DirectoryInfo(dir);
     
                TreeNode newNode = this.tv.Nodes.Add(di.Name);
                newNode.Tag = di.FullName;
                newNode.StateImageIndex = 0;
                LoadFiles(dir, newNode);
                LoadSubDirectories(dir, newNode);
     
            }
            private void LoadSubDirectories(string dir, TreeNode node)
            {
                string[] subdirectoryEntries = Directory.GetDirectories(dir);
                // Loop through them to see if they have any other subdirectories  
                foreach (string subdirectory in subdirectoryEntries)
                {
                    DirectoryInfo di = new DirectoryInfo(subdirectory);
                    TreeNode newNnode = node.Nodes.Add(di.Name);
                    newNnode.StateImageIndex = 0;
                    newNnode.Tag = di.FullName;
                    LoadFiles(subdirectory, newNnode);
                    LoadSubDirectories(subdirectory, newNnode);
     
     
                }
            }
            private void LoadFiles(string dir, TreeNode td)
            {
                string[] files = Directory.GetFiles(dir, "*.txt");
     
                // Loop through them to see files  
                foreach (string file in files)
                {
                    FileInfo fi = new FileInfo(file);
                    TreeNode tnode = td.Nodes.Add(fi.Name);
                    tnode.Tag = fi.FullName;
                    tnode.StateImageIndex = 1;
                }
     
            }
     
            private void menuNewItem_Click(object sender, EventArgs e)
            {
                if (this.tv.SelectedNode == null) return;
     
                string pathDir = (string)this.tv.SelectedNode.Tag;
                DirectoryInfo di = new DirectoryInfo(pathDir);
     
                // add file to  di
                string newFileName = GetNewFileName(di);
     
                // Adds new node as a child node of the currently selected node.
                TreeNode newNode = new TreeNode(newFileName);
                newNode.Tag = di.FullName + @"\\" + newFileName;
                newNode.ImageKey = "NewFile";
                this.tv.SelectedNode.Nodes.Add(newNode);
     
            }
     
            private void menuNewFolder_Click(object sender, EventArgs e)
            {
                if (this.tv.SelectedNode == null) return;
     
                TreeNode selNode = this.tv.SelectedNode;
     
                string pathDir = (string)selNode.Tag;
                DirectoryInfo di = new DirectoryInfo(pathDir);
     
                // add subdirectory to  di
                string newDirectoryName = GetNewFolderName(di);
     
                // Adds new node as a child node of the currently selected node.
                TreeNode newNode = new TreeNode(newDirectoryName);
                newNode.Tag = di.FullName + @"\\" + newDirectoryName;
                newNode.ImageKey = "NewFolder";
     
                selNode.Nodes.Add(newNode);
            }
     
            private void menuOpenFile_Click(object sender, EventArgs e)
            {
                if (this.tv.SelectedNode == null) return;
     
                TreeNode selNode = this.tv.SelectedNode;
     
                if (selNode.Text.EndsWith("txt"))
                {
                    string pathDir = (string)selNode.Tag;
                    FileInfo fi = new FileInfo(pathDir);
     
                    // edit file
                    using (StreamReader sr = new StreamReader(fi.FullName))
                    {
                        BoxEditor.Clear();
                        BoxEditor.Text = sr.ReadToEnd();
     
                        tabControl1.SelectedIndex = 1;
                        tabPage2.Show();
                    }
                }
     
     
            }
            private void menuDelete_Click(object sender, EventArgs e)
            {
                if (this.tv.SelectedNode == null) return;
     
                TreeNode selNode = this.tv.SelectedNode;
     
                string pathDir = (string)selNode.Tag;
                if (pathDir.Contains("txt") || pathDir.Contains("xml"))
                {
                    FileInfo fi = new FileInfo(pathDir);
                    fi.Delete();
                }
                else
                {
                    DirectoryInfo di = new DirectoryInfo(pathDir);
                    di.Delete(true);
                }
     
                // Removes currently selected node
                this.tv.Nodes.Remove(selNode);
     
            }
            private string GetNewFileName(DirectoryInfo di)
            {
                string newItem = "NewItem";
                string[] files = Directory.GetFiles(di.FullName, "*.txt");
                string fileName = di.FullName + @"\" + newItem;
                int countFile = 0;
                if (files.Length == 0)
                    countFile = 1;
                else
                    countFile = files.Length + 1;
     
                fileName += countFile.ToString() + ".txt";
     
                FileInfo fi = new FileInfo(fileName);
     
                //Create a file .
                using (StreamWriter sw = fi.CreateText())
                {
                    sw.WriteLine(newItem);
                    sw.WriteLine("Hello");
                    sw.WriteLine("And");
                    sw.WriteLine("Welcome");
                }
                newItem += countFile.ToString() + ".txt";
                return newItem;
     
            }
            private string GetNewFolderName(DirectoryInfo di)
            {
                string newFolder = "NewFolder";
                string[] subdirs = Directory.GetDirectories(di.FullName, newFolder + "*");
                string dirName = di.FullName + @"\\" + newFolder;
                int countDir = 0;
                if (subdirs.Length == 0)
                    countDir = 1;
                else
                    countDir = subdirs.Length + 1;
     
     
                dirName += countDir.ToString();
                DirectoryInfo newDir = new DirectoryInfo(dirName);
     
                //Create a  directory
                newDir.Create();
     
                newFolder += countDir.ToString();
                return newFolder;
     
            }
     
            // ADDENDUM CODE POUR RENOMMER UN DIRECTORYINF OU UN FILEINFO
            // Extrait de la MSDN Library fr
            /* Grab the tree node under the mouse pointer and 
              save it in the mySelectedNode variable. */
            private TreeNode mySelectedNode = null;
            private void tv_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
            {
                mySelectedNode = tv.GetNodeAt(e.X, e.Y);
            }
            private void menuItemRename_Click(object sender, EventArgs e)
            {
     
                if (mySelectedNode != null && mySelectedNode.Parent != null)
                {
                    this.tv.SelectedNode = mySelectedNode;
                    this.tv.LabelEdit = true;
                    if (!mySelectedNode.IsEditing)
                    {
                        mySelectedNode.BeginEdit();
                    }
                }
                else
                {
                    MessageBox.Show("No tree node selected or selected node is a root node.\n" +
                       "Editing of root nodes is not allowed.", "Invalid selection");
                }
            }
     
            private void tv_AfterLabelEdit(object sender, System.Windows.Forms.NodeLabelEditEventArgs e)
            {
                if (e.Label != null)
                {
                    if (e.Label.Length > 0)
                    {
                        if (e.Label.IndexOfAny(new char[] { '@', '\\', ',', '!' }) == -1)
                        {
                            // Stop editing without canceling the label change.
                            RenameItem(e.Node, e.Label);
                            e.Node.EndEdit(false);
     
                        }
                        else
                        {
                            /* Cancel the label edit action, inform the user, and 
                               place the node in edit mode again. */
                            e.CancelEdit = true;
                            MessageBox.Show("Invalid tree node label.\n" +
                               "The invalid characters are: '@','.', ',', '!'",
                               "Node Label Edit");
                            e.Node.BeginEdit();
                        }
                    }
                    else
                    {
                        /* Cancel the label edit action, inform the user, and 
                           place the node in edit mode again. */
                        e.CancelEdit = true;
                        MessageBox.Show("Invalid tree node label.\nThe label cannot be blank",
                           "Node Label Edit");
                        e.Node.BeginEdit();
                    }
                    this.tv.LabelEdit = false;
                }
            }
     
            private string GetNewFileName(DirectoryInfo di)
            {
                string newItem = "NewItem";
                string[] files = Directory.GetFiles(di.FullName, "*.txt");
                string fileName = di.FullName + @"\" + newItem;
                int countFile = 0;
                if (files.Length == 0)
                    countFile = 1;
                else
                    countFile = files.Length + 1;
     
                fileName += countFile.ToString() + ".txt";
     
                FileInfo fi = new FileInfo(fileName);
     
                //Create a file .
                using (StreamWriter sw = fi.CreateText())
                {
                    sw.WriteLine(newItem);
                    sw.WriteLine("Hello");
                    sw.WriteLine("And");
                    sw.WriteLine("Welcome");
                }
                newItem += countFile.ToString() + ".txt";
                return newItem;
     
            }
            // Utiles Functions
            private string GetNewFolderName(DirectoryInfo di)
            {
                string newFolder = "NewFolder";
                string[] subdirs = Directory.GetDirectories(di.FullName, newFolder + "*");
                string dirName = di.FullName + @"\\" + newFolder;
                int countDir = 0;
                if (subdirs.Length == 0)
                    countDir = 1;
                else
                    countDir = subdirs.Length + 1;
     
     
                dirName += countDir.ToString();
                DirectoryInfo newDir = new DirectoryInfo(dirName);
     
                //Create a  directory
                newDir.Create();
     
                newFolder += countDir.ToString();
                return newFolder;
     
            }
            private void RenameItem(TreeNode node, string newLabel)
            {
                string oldPathDir = (string)node.Tag;
                if (oldPathDir.Contains("txt") || oldPathDir.Contains("xml"))// it is a fileInfo
                {
                    FileInfo srcFi = new FileInfo(oldPathDir);
                    string srcFileName = srcFi.FullName;
                    int index = srcFileName.LastIndexOf('\\');
                    string partFileName = srcFileName.Remove(index);
     
                    string dstFileName = partFileName + @"\\" + newLabel;
     
                    if (!srcFileName.Equals(  dstFileName))
                    {
                        srcFi.MoveTo(dstFileName);// move source to destination FileInfo
     
                        srcFi.Delete();// delete source FileInfo
                    }
                }
                else // it is a directorinfo
                {
     
                    DirectoryInfo srcDir = new DirectoryInfo(oldPathDir);
     
                    string srcDirName = srcDir.FullName;
                    int index = srcDirName.LastIndexOf('\\');
                    string partFolderName = srcDirName.Remove(index);
     
                    string dstDirName = partFolderName + @"\\" + newLabel;
                    if ( ! srcDirName.Equals(dstDirName))
                    {
                        srcDir.MoveTo(dstDirName);// move source to destination directoryinfo
     
                        srcDir.Delete(true);// delete source directoryinfo}
                    }
     
     
                }
     
     
     
     
     
            }
        }
    }
    bon code...

  14. #14
    Membre extrêmement actif
    Inscrit en
    Avril 2008
    Messages
    2 573
    Détails du profil
    Informations personnelles :
    Âge : 65

    Informations forums :
    Inscription : Avril 2008
    Messages : 2 573
    Par défaut
    Citation Envoyé par M.Leroy Voir le message


    Et comment personnaliser les icônes de la tree view en fonction de si c'est un noeud dossier ou un noeud de fichier .txt .xml histoire d'avoir des icônes persos.
    et pourquoi pas après la faculté de copier coller.
    Merci si vous avez des idées de code en parallèle de mes recherches.
    Pour personnaliser le TV avec des icones ou images,ImageList est ton ami.
    Encore un extrait de la mine d' "or" de la MSDN Lib fr qui ne m'a pas enrichi ,alas :
    Pour afficher des images à côté des nœuds d'arbre, assignez un ImageList à la propriété ImageList et faites référence à la valeur d'index d'un Image dans le ImageList pour assigner Image. Utilisez les propriétés suivantes pour assigner des images :

    Affectez à la propriété ImageIndex la valeur d'index du Image que vous souhaitez afficher lorsqu' un nœud d'arbre est non sélectionné.

    Affectez à la propriété SelectedImageIndex la valeur d'index de Image que vous souhaitez afficher lorsqu' un nœud d'arbre est sélectionné




    bon code.

  15. #15
    Membre averti Avatar de M.Leroy
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Février 2019
    Messages
    49
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Février 2019
    Messages : 49
    Par défaut
    Bonjour et merci
    cependant lorsque je renomme un fichier ou un dossier, celui-ci disparait définitivement après l'avoir renommé (pas de la tree view mais quand j'ouvre l'explorateur de fichier windows je vois qu'il existe plus)
    voici le code de ma fenêtre
    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
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
     
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
     
     
    namespace Explorateur
    {
        public partial class WindowsFormsApplication1 : Form
        {
            private string path = "E:/" ;
            public WindowsFormsApplication1()
            {
                InitializeComponent();
            }
     
            private void WindowsFormsApplication1_Load(object sender, EventArgs e)
            {
                this.tv.ContextMenuStrip = this.contextMenuStrip1;
                // Clear All Nodes if Already Exists  
                this.tv.Nodes.Clear();
     
                if (path != "" && Directory.Exists(path))
                    LoadDirectory(path);
     
            }
     
            private void LoadDirectory(string dir)
            {
                DirectoryInfo di = new DirectoryInfo(dir);
     
                TreeNode newNode = this.tv.Nodes.Add(di.Name);
                newNode.Tag = di.FullName;
                newNode.StateImageIndex = 0;
                LoadFiles(dir, newNode);
                LoadSubDirectories(dir, newNode);
     
            }
            private void LoadSubDirectories(string dir, TreeNode node)
            {
                string[] subdirectoryEntries = Directory.GetDirectories(dir);
                // Loop through them to see if they have any other subdirectories  
                foreach (string subdirectory in subdirectoryEntries)
                {
                    DirectoryInfo di = new DirectoryInfo(subdirectory);
                    TreeNode newNnode = node.Nodes.Add(di.Name);
                    newNnode.StateImageIndex = 0;
                    newNnode.Tag = di.FullName;
                    LoadFiles(subdirectory, newNnode);
                    LoadSubDirectories(subdirectory, newNnode);
     
     
                }
            }
            private void LoadFiles(string dir, TreeNode td)
            {
                string[] files = Directory.GetFiles(dir, "*.txt");
     
                // Loop through them to see files  
                foreach (string file in files)
                {
                    FileInfo fi = new FileInfo(file);
                    TreeNode tnode = td.Nodes.Add(fi.Name);
                    tnode.Tag = fi.FullName;
                    tnode.StateImageIndex = 1;
                }
     
            }
            private void newItemToolStripMenuItem_Click(object sender, EventArgs e)
            {
                if (this.tv.SelectedNode == null) return;
     
                TreeNode selNode = this.tv.SelectedNode;
     
                string pathDir = (string)selNode.Tag;
                DirectoryInfo di = new DirectoryInfo(pathDir);
     
                // add subdirectory to  di
                string newDirectoryName = GetNewFolderName(di);
     
                // Adds new node as a child node of the currently selected node.
                TreeNode newNode = new TreeNode(newDirectoryName);
                newNode.Tag = di.FullName + @"\\" + newDirectoryName;
                newNode.ImageKey = "NewFolder";
     
                selNode.Nodes.Add(newNode);
            }
     
     
     
            private void openFileToolStripMenuItem_Click(object sender, EventArgs e)
            {
                if (this.tv.SelectedNode == null) return;
     
                TreeNode selNode = this.tv.SelectedNode;
     
                if (selNode.Text.EndsWith("cpc"))
                {
                    string pathDir = (string)selNode.Tag;
                    FileInfo fi = new FileInfo(pathDir);
     
                    // edit file
                    using (StreamReader sr = new StreamReader(fi.FullName))
                    {
     
                        BoxEditor.Clear();
                        BoxEditor.Text = sr.ReadToEnd();
     
                        tabControl1.SelectedIndex = 1;
                        tabPage2.Show();
                    }
                }
            }
     
            private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
            {
                if (this.tv.SelectedNode == null) return;
     
                TreeNode selNode = this.tv.SelectedNode;
     
                string pathDir = (string)selNode.Tag;
                if (pathDir.Contains("cpc") || pathDir.Contains("xml"))
                {
                    FileInfo fi = new FileInfo(pathDir);
                    fi.Delete();
                }
                else
                {
                    DirectoryInfo di = new DirectoryInfo(pathDir);
                    di.Delete(true);
                }
     
                // Removes currently selected node
                this.tv.Nodes.Remove(selNode);
            }
            private string GetNewFileNameCPC(DirectoryInfo di)
            {
                string newItem = "NewItem";
                string[] files = Directory.GetFiles(di.FullName, "*.cpc");
                string fileName = di.FullName + @"\" + newItem;
                int countFile = 0;
                if (files.Length == 0)
                    countFile = 1;
                else
                    countFile = files.Length + 1;
     
                fileName += countFile.ToString() + ".cpc";
     
                FileInfo fi = new FileInfo(fileName);
     
                //Create a file .
                using (StreamWriter sw = fi.CreateText())
                {
                    sw.WriteLine(newItem);
                    sw.WriteLine("Hello");
                    sw.WriteLine("And");
                    sw.WriteLine("Welcome");
                }
                newItem += countFile.ToString() + ".cpc";
                return newItem;
     
            }
     
            private string GetNewFileNameTXT(DirectoryInfo di)
            {
                string newItem = "NewItem";
                string[] files = Directory.GetFiles(di.FullName, "*.txt");
                string fileName = di.FullName + @"\" + newItem;
                int countFile = 0;
                if (files.Length == 0)
                    countFile = 1;
                else
                    countFile = files.Length + 1;
     
                fileName += countFile.ToString() + ".txt";
     
                FileInfo fi = new FileInfo(fileName);
     
                //Create a file .
                using (StreamWriter sw = fi.CreateText())
                {
                    sw.WriteLine(newItem);
                    sw.WriteLine("Hello");
                    sw.WriteLine("And");
                    sw.WriteLine("Welcome");
                }
                newItem += countFile.ToString() + ".txt";
                return newItem;
     
            }
            private string GetNewFolderName(DirectoryInfo di)
            {
                string newFolder = "NewFolder";
                string[] subdirs = Directory.GetDirectories(di.FullName, newFolder + "*");
                string dirName = di.FullName + @"\\" + newFolder;
                int countDir = 0;
                if (subdirs.Length == 0)
                    countDir = 1;
                else
                    countDir = subdirs.Length + 1;
     
     
                dirName += countDir.ToString();
                DirectoryInfo newDir = new DirectoryInfo(dirName);
     
                //Create a  directory
                newDir.Create();
     
                newFolder += countDir.ToString();
                return newFolder;
     
            }
     
            private void button1_Click(object sender, EventArgs e)
            {
                FolderBrowserDialog dlg = new FolderBrowserDialog();
     
                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    txtDirectory.Text = dlg.SelectedPath;
                    this.tv.Nodes.Clear();
                    LoadDirectory(txtDirectory.Text);
                   
                }
            }
     
            private void tv_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
            {
                if (this.tv.SelectedNode == null) return;
     
                TreeNode selNode = this.tv.SelectedNode;
     
                if (selNode.Text.EndsWith("cpc")) 
                {
                    string pathDir = (string)selNode.Tag;
                    FileInfo fi = new FileInfo(pathDir);
     
                    // edit file
                    using (StreamReader sr = new StreamReader(fi.FullName))
                    {
                        BoxEditor.Clear();
                        BoxEditor.Text = sr.ReadToEnd();
     
                        tabControl1.SelectedIndex = 1;
                        tabPage2.Show();
                    }
                }
                if (selNode.Text.EndsWith("CPC"))
                {
                    string pathDir = (string)selNode.Tag;
                    FileInfo fi = new FileInfo(pathDir);
     
                    // edit file
                    using (StreamReader sr = new StreamReader(fi.FullName))
                    {
                        BoxEditor.Clear();
                        BoxEditor.Text = sr.ReadToEnd();
     
                        tabControl1.SelectedIndex = 1;
                        tabPage2.Show();
                    }
                }
            }
     
            private void cpcdosCToolStripMenuItem_Click(object sender, EventArgs e)
            {
                if (this.tv.SelectedNode == null) return;
     
                string pathDir = (string)this.tv.SelectedNode.Tag;
                DirectoryInfo di = new DirectoryInfo(pathDir);
     
                // add file to  di
                string newFileName = GetNewFileNameCPC(di);
     
                // Adds new node as a child node of the currently selected node.
                TreeNode newNode = new TreeNode(newFileName);
                newNode.Tag = di.FullName + @"\\" + newFileName;
                newNode.ImageKey = "NewFile";
              
                this.tv.SelectedNode.Nodes.Add(newNode);
            }
     
            private void texteToolStripMenuItem_Click(object sender, EventArgs e)
            {
                if (this.tv.SelectedNode == null) return;
     
                string pathDir = (string)this.tv.SelectedNode.Tag;
                DirectoryInfo di = new DirectoryInfo(pathDir);
     
                // add file to  di
                string newFileName = GetNewFileNameTXT(di);
     
                // Adds new node as a child node of the currently selected node.
                TreeNode newNode = new TreeNode(newFileName);
                newNode.Tag = di.FullName + @"\\" + newFileName;
                newNode.ImageKey = "NewFile";
     
                this.tv.SelectedNode.Nodes.Add(newNode);
            }
            // ADDENDUM CODE POUR RENOMMER UN DIRECTORYINF OU UN FILEINFO
            // Extrait de la MSDN Library fr
            /* Grab the tree node under the mouse pointer and 
              save it in the mySelectedNode variable. */
            private TreeNode mySelectedNode = null;
            private void tv_MouseDown(object sender, MouseEventArgs e)
            {
                mySelectedNode = tv.GetNodeAt(e.X, e.Y);
            }
     
            private void renommerToolStripMenuItem_Click(object sender, EventArgs e)
            {
                if (mySelectedNode != null && mySelectedNode.Parent != null)
                {
                    this.tv.SelectedNode = mySelectedNode;
                    this.tv.LabelEdit = true;
                    if (!mySelectedNode.IsEditing)
                    {
                        mySelectedNode.BeginEdit();
                    }
                }
                else
                {
                    MessageBox.Show("No tree node selected or selected node is a root node.\n" +
                       "Editing of root nodes is not allowed.", "Invalid selection");
                }
            }
     
            private void tv_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
            {
                if (e.Label != null)
                {
                    if (e.Label.Length > 0)
                    {
                        if (e.Label.IndexOfAny(new char[] { '@', '\\', ',', '!' }) == -1)
                        {
                            // Stop editing without canceling the label change.
                            RenameItem(e.Node, e.Label);
                            e.Node.EndEdit(false);
     
                        }
                        else
                        {
                            /* Cancel the label edit action, inform the user, and 
                               place the node in edit mode again. */
                            e.CancelEdit = true;
                            MessageBox.Show("Invalid tree node label.\n" +
                               "The invalid characters are: '@','.', ',', '!'",
                               "Node Label Edit");
                            e.Node.BeginEdit();
                        }
                    }
                    else
                    {
                        /* Cancel the label edit action, inform the user, and 
                           place the node in edit mode again. */
                        e.CancelEdit = true;
                        MessageBox.Show("Invalid tree node label.\nThe label cannot be blank",
                           "Node Label Edit");
                        e.Node.BeginEdit();
                    }
                    this.tv.LabelEdit = false;
                }
            }
            private void RenameItem(TreeNode node, string newLabel)
            {
                string oldPathDir = (string)node.Tag;
                if (oldPathDir.Contains("txt") || oldPathDir.Contains("xml"))// it is a fileInfo
                {
                    FileInfo srcFi = new FileInfo(oldPathDir);
                    string srcFileName = srcFi.FullName;
                    int index = srcFileName.LastIndexOf('\\');
                    string partFileName = srcFileName.Remove(index);
     
                    string dstFileName = partFileName + @"\\" + newLabel;
     
                    if (!srcFileName.Equals(dstFileName))
                    {
                        srcFi.MoveTo(dstFileName);// move source to destination FileInfo
     
                        srcFi.Delete();// delete source FileInfo
                    }
                }
                else // it is a directorinfo
                {
     
                    DirectoryInfo srcDir = new DirectoryInfo(oldPathDir);
     
                    string srcDirName = srcDir.FullName;
                    int index = srcDirName.LastIndexOf('\\');
                    string partFolderName = srcDirName.Remove(index);
     
                    string dstDirName = partFolderName + @"\\" + newLabel;
                    if (!srcDirName.Equals(dstDirName))
                    {
                        srcDir.MoveTo(dstDirName);// move source to destination directoryinfo
     
                        srcDir.Delete(true);// delete source directoryinfo}
                    }
     
     
                }
     
     
     
            }
        }
    }
    Merci

  16. #16
    Membre extrêmement actif
    Inscrit en
    Avril 2008
    Messages
    2 573
    Détails du profil
    Informations personnelles :
    Âge : 65

    Informations forums :
    Inscription : Avril 2008
    Messages : 2 573
    Par défaut
    Rebonjour

    Il y a un problème dans ton code car comme tous les débutants tu a modifié un code sans te rendre des conséquences induites .
    Il s'agit de l'event tv_NodeMouseDoubleClic que tu a rajouté & qu'il faut VIRER.
    Cet event va occulter l'event tv_MouseDown utilisé pour renommer l’étiquette de TreeNode.
    Car tv_NodeMouseDoubleClick n'est qu'un proxy de tv_Click ou ce qui revient au même tv_MouseDown,le class TreeNode n’étant pas un Control capable d'intercepter les clics de souris.
    Ceci foutra une sacrée pagaille dans l'interface Treeview donnée.

    Pour le renommage des files et folders j'ai remplacé les méthodes d'instance de
    "Move" de DirectoryInfo et FileInfo par la méthode statique "Move " du Class Directory.
    code revu behind.cs:

    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
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
    456
    457
    458
    459
    460
    461
    462
    463
    464
    465
    466
    467
    468
    469
    470
    471
    472
    473
    474
    475
    476
    477
    478
    479
    480
    481
    482
    483
    484
     
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
     
    namespace Explorateur
    {
        public partial class WindowsFormsApplication1 : Form
        {
           private string path = "E:/" ;
            public WindowsFormsApplication1()
            {
                InitializeComponent();
                this.tv.ContextMenuStrip = this.contextMenuStrip1;
     
     
                //  EVENTS du TV
                this.tv.MouseDown+=new MouseEventHandler(tv_MouseDown); 
                this.tv.AfterLabelEdit+=new NodeLabelEditEventHandler(tv_AfterLabelEdit);
     
     
            }
     
            //private void WindowsFormsApplication1_Load(object sender, EventArgs e)
            //{
            //    this.tv.ContextMenuStrip = this.contextMenuStrip1;
            //    // Clear All Nodes if Already Exists  
            //    this.tv.Nodes.Clear();
     
            //    if (path != "" && Directory.Exists(path))
            //        LoadDirectory(path);
     
            //}
            private void btnSelectFolder_Click(object sender, EventArgs e)
            {
                FolderBrowserDialog dlg = new FolderBrowserDialog();
     
                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    path = dlg.SelectedPath; 
                    txtDirectory.Text = path ;
     
                    this.tv.Nodes.Add(new TreeNode("Explorateur"));
                    LoadDirectory(path);
     
                }
            }
            private void LoadDirectory(string dir)
            {
                DirectoryInfo di = new DirectoryInfo(dir);
     
                TreeNode newNode = this.tv.Nodes.Add(di.Name);
                newNode.Tag = di.FullName;
                newNode.StateImageIndex = 0;
                LoadFiles(dir, newNode);
                LoadSubDirectories(dir, newNode);
     
            }
            private void LoadSubDirectories(string dir, TreeNode node)
            {
                string[] subdirectoryEntries = Directory.GetDirectories(dir);
                // Loop through them to see if they have any other subdirectories  
                foreach (string subdirectory in subdirectoryEntries)
                {
                    DirectoryInfo di = new DirectoryInfo(subdirectory);
                    TreeNode newNnode = node.Nodes.Add(di.Name);
                    newNnode.StateImageIndex = 0;
                    newNnode.Tag = di.FullName;
                    LoadFiles(subdirectory, newNnode);
                    LoadSubDirectories(subdirectory, newNnode);
     
     
                }
            }
            // MODIF POUR AFFICHER LES FICHIERS *.cpc
            private void LoadFiles(string dir, TreeNode td)
            {
                string[] files = Directory.GetFiles(dir, "*.txt");
     
                // Loop through them to see files  
                foreach (string file in files)
                {
                    FileInfo fi = new FileInfo(file);
                    TreeNode tnode = td.Nodes.Add(fi.Name);
                    tnode.Tag = fi.FullName;
                    tnode.StateImageIndex = 1;
                }
     
                // AJOUT
                files = Directory.GetFiles(dir, "*.cpc");
                // Loop through them to see files  
                foreach (string file in files)
                {
                    FileInfo fi = new FileInfo(file);
                    TreeNode tnode = td.Nodes.Add(fi.Name);
                    tnode.Tag = fi.FullName;
                    tnode.StateImageIndex = 2;
                }
            }
            private void NewFolderItemToolStripMenuItem_Click(object sender, EventArgs e)
            {
     
                if (this.tv.SelectedNode == null) return;
     
                TreeNode selNode = this.tv.SelectedNode;
     
                string pathDir = (string)selNode.Tag;
                DirectoryInfo di = new DirectoryInfo(pathDir);
     
                // add subdirectory to  di
                string newDirectoryName = GetNewFolderName(di);
     
                // Adds new node as a child node of the currently selected node.
                TreeNode newNode = new TreeNode(newDirectoryName);
                newNode.Tag = di.FullName + @"\\" + newDirectoryName;
                newNode.ImageKey = "NewFolder";
     
                selNode.Nodes.Add(newNode);
            }
     
     
     
     
            private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
            {
                if (this.tv.SelectedNode == null) return;
     
                TreeNode selNode = this.tv.SelectedNode;
     
                string pathDir = (string)selNode.Tag;
                if (pathDir.Contains("cpc") || pathDir.Contains("xml"))
                {
                    FileInfo fi = new FileInfo(pathDir);
                    fi.Delete();
                }
                else
                {
                    DirectoryInfo di = new DirectoryInfo(pathDir);
                    di.Delete(true);
                }
     
                // Removes currently selected node
                this.tv.Nodes.Remove(selNode);
            }
            private string GetNewFileNameCPC(DirectoryInfo di)
            {
                string newItem = "NewItem";
                string[] files = Directory.GetFiles(di.FullName, "*.cpc");
                string fileName = di.FullName + @"\" + newItem;
                int countFile = 0;
                if (files.Length == 0)
                    countFile = 1;
                else
                    countFile = files.Length + 1;
     
                fileName += countFile.ToString() + ".cpc";
     
                FileInfo fi = new FileInfo(fileName);
     
                //Create a file .
                using (StreamWriter sw = fi.CreateText())
                {
                    sw.WriteLine(newItem);
                    sw.WriteLine("Hello");
                    sw.WriteLine("And");
                    sw.WriteLine("Welcome");
                }
                newItem += countFile.ToString() + ".cpc";
                return newItem;
     
            }
     
            private string GetNewFileNameTXT(DirectoryInfo di)
            {
                string newItem = "NewItem";
                string[] files = Directory.GetFiles(di.FullName, "*.txt");
                string fileName = di.FullName + @"\" + newItem;
                int countFile = 0;
                if (files.Length == 0)
                    countFile = 1;
                else
                    countFile = files.Length + 1;
     
                fileName += countFile.ToString() + ".txt";
     
                FileInfo fi = new FileInfo(fileName);
     
                //Create a file .
                using (StreamWriter sw = fi.CreateText())
                {
                    sw.WriteLine(newItem);
                    sw.WriteLine("Hello");
                    sw.WriteLine("And");
                    sw.WriteLine("Welcome");
                }
                newItem += countFile.ToString() + ".txt";
                return newItem;
     
            }
            private string GetNewFolderName(DirectoryInfo di)
            {
                string newFolder = "NewFolder";
                string[] subdirs = Directory.GetDirectories(di.FullName, newFolder + "*");
                string dirName = di.FullName + @"\\" + newFolder;
                int countDir = 0;
                if (subdirs.Length == 0)
                    countDir = 1;
                else
                    countDir = subdirs.Length + 1;
     
     
                dirName += countDir.ToString();
                DirectoryInfo newDir = new DirectoryInfo(dirName);
     
                //Create a  directory
                newDir.Create();
     
                newFolder += countDir.ToString();
                return newFolder;
     
            }
     
            // CODE A VIRER  CAR IL FOUT LA PAGAILLE ET OCCULTE LE DECLENCHEMENT
            // DE L'EVENT  TREEVIEW_MOUSEDOWN UTILISE POUR RENOMMER L'ETIQUETTE
            // DE NOEUD   (et des objets afferents DirectoryInfo et FilInfo)
            // L'EVENT openFileCPCToolStripMenuItem FAIT LE BOULOT
     
            //private void tv_NodeMouseDoubleClic(object sender, MouseEventArgs e)
            //{
     
            //    if (this.tv.SelectedNode == null) return;
     
            //    TreeNode selNode = this.tv.SelectedNode;
     
            //    if (selNode.Text.EndsWith("cpc") ) 
            //    {
            //        string pathDir = (string)selNode.Tag;
            //        FileInfo fi = new FileInfo(pathDir);
     
            //        // edit file
            //        using (StreamReader sr = new StreamReader(fi.FullName))
            //        {
            //            BoxEditor.Clear();
            //            BoxEditor.Text = sr.ReadToEnd();
     
            //            tabControl1.SelectedIndex = 1;
            //            tabPage2.Show();
            //        }
            //    }
            //   // A VIRER
     
            //    if (selNode.Text.EndsWith("CPC"))
            //    {
            //        string pathDir = (string)selNode.Tag;
            //        FileInfo fi = new FileInfo(pathDir);
     
            //        // edit file
            //        using (StreamReader sr = new StreamReader(fi.FullName))
            //        {
            //            BoxEditor.Clear();
            //            BoxEditor.Text = sr.ReadToEnd();
     
            //            tabControl1.SelectedIndex = 1;
            //            tabPage2.Show();
            //        }
            //    }
            //}
            private void openFileCPCToolStripMenuItem_Click(object sender, EventArgs e)
            {
                if (this.tv.SelectedNode == null) return;
     
                TreeNode selNode = this.tv.SelectedNode;
     
                if (selNode.Text.EndsWith("cpc") || selNode.Text.EndsWith("CPC"))
                {
                    string pathDir = (string)selNode.Tag;
                    FileInfo fi = new FileInfo(pathDir);
     
                    // edit file
                    using (StreamReader sr = new StreamReader(fi.FullName))
                    {
                        BoxEditor.Clear();
                        BoxEditor.Text = sr.ReadToEnd();
     
                        tabControl1.SelectedIndex = 1;
                        tabPage2.Show();
                    }
                }
            }
     
     
            private void openFileTextToolStripMenuItem_Click(object sender, EventArgs e)
            {
                if (this.tv.SelectedNode == null) return;
     
                TreeNode selNode = this.tv.SelectedNode;
     
                if (selNode.Text.EndsWith("txt") || selNode.Text.EndsWith("TEXT"))
                {
                    string pathDir = (string)selNode.Tag;
                    FileInfo fi = new FileInfo(pathDir);
     
                    // edit file
                    using (StreamReader sr = new StreamReader(fi.FullName))
                    {
                        BoxEditor.Clear();
                        BoxEditor.Text = sr.ReadToEnd();
     
                        tabControl1.SelectedIndex = 1;
                        tabPage2.Show();
                    }
                }
     
            }
            private void newFileCPCToolStripMenuItem_Click(object sender, EventArgs e)
            {
     
     
                if (this.tv.SelectedNode == null) return;
     
                string pathDir = (string)this.tv.SelectedNode.Tag;
                DirectoryInfo di = new DirectoryInfo(pathDir);
     
                // add file to  di
                string newFileName = GetNewFileNameCPC(di);
     
                // Adds new node as a child node of the currently selected node.
                TreeNode newNode = new TreeNode(newFileName);
                newNode.Tag = di.FullName + @"\\" + newFileName;
                newNode.ImageKey = "NewFile";
     
                this.tv.SelectedNode.Nodes.Add(newNode);
            }
            private void newFileTextToolStripMenuItem_Click(object sender, EventArgs e)
            {
                if (this.tv.SelectedNode == null) return;
     
                string pathDir = (string)this.tv.SelectedNode.Tag;
                DirectoryInfo di = new DirectoryInfo(pathDir);
     
                // add file to  di
                string newFileName = GetNewFileNameTXT(di);
     
                // Adds new node as a child node of the currently selected node.
                TreeNode newNode = new TreeNode(newFileName);
                newNode.Tag = di.FullName + @"\\" + newFileName;
                newNode.ImageKey = "NewFile";
     
                this.tv.SelectedNode.Nodes.Add(newNode);
            }
     
     
     
            // ADDENDUM CODE POUR RENOMMER UN DIRECTORYINF OU UN FILEINFO
            // Extrait de la MSDN Library fr
            /* Grab the tree node under the mouse pointer and 
              save it in the mySelectedNode variable. */
     
            private TreeNode mySelectedNode = null;
            private void tv_MouseDown(object sender, MouseEventArgs e)
            {
                mySelectedNode = tv.GetNodeAt(e.X, e.Y);
            }
     
            private void renameToolStripMenuItem_Click(object sender, EventArgs e)
            {
     
                if (mySelectedNode != null && mySelectedNode.Parent != null)
                {
                    this.tv.SelectedNode = mySelectedNode;
                    this.tv.LabelEdit = true;
                    if (!mySelectedNode.IsEditing)
                    {
                        mySelectedNode.BeginEdit();
                    }
                }
                else
                {
                    MessageBox.Show("No tree node selected or selected node is a root node.\n" +
                       "Editing of root nodes is not allowed.", "Invalid selection");
                }
            }
     
            private void tv_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
            {
                if (e.Label != null)
                {
                    if (e.Label.Length > 0)
                    {
                        if (e.Label.IndexOfAny(new char[] { '@', '\\', ',', '!' }) == -1)
                        {
                            // Stop editing without canceling the label change.
                            RenameItem(e.Node, e.Label);
                            e.Node.EndEdit(false);
     
                        }
                        else
                        {
                            /* Cancel the label edit action, inform the user, and 
                               place the node in edit mode again. */
                            e.CancelEdit = true;
                            MessageBox.Show("Invalid tree node label.\n" +
                               "The invalid characters are: '@','.', ',', '!'",
                               "Node Label Edit");
                            e.Node.BeginEdit();
                        }
                    }
                    else
                    {
                        /* Cancel the label edit action, inform the user, and 
                           place the node in edit mode again. */
                        e.CancelEdit = true;
                        MessageBox.Show("Invalid tree node label.\nThe label cannot be blank",
                           "Node Label Edit");
                        e.Node.BeginEdit();
                    }
                    this.tv.LabelEdit = false;
                }
            }
            // MODIF A AJOUTER " || oldPathDir.Contains("cpc")"
            private void RenameItem(TreeNode node, string newLabel)
            {
                string oldPathDir = (string)node.Tag;
                if (oldPathDir.Contains("txt") ||
                    oldPathDir.Contains("xml")||
                    oldPathDir.Contains("cpc"))// it is a fileInfo
                {
                    FileInfo srcFi = new FileInfo(oldPathDir);
                    string srcDircName = srcFi.FullName;
                    int index = srcDircName.LastIndexOf('\\');
                    string partDirName = srcDircName.Remove(index);
     
                    string dstDirName = partDirName + @"\\" + newLabel;
     
                    if (!srcDircName.Equals(dstDirName))
                    {
                        Directory.Move(srcDircName, dstDirName);// move source to destination 
                        
                        //CODE A VIRER
                        //srcFi.MoveTo(dstFileName);
     
                        //srcFi.Delete();
                       
                    }
                }
                else // it is a directorinfo
                {
     
                    DirectoryInfo srcDir = new DirectoryInfo(oldPathDir);
     
                    string srcDirName = srcDir.FullName;
                    int index = srcDirName.LastIndexOf('\\');
                    string partDirName = srcDirName.Remove(index);
     
                    string dstDirName = partDirName + @"\\" + newLabel;
                    if (!srcDirName.Equals(dstDirName))
                    {
                        Directory.Move(srcDirName, dstDirName);// move source to destination 
     
                        //CODE A VIRER
                        
                        //srcDir.MoveTo(dstDirName);
     
                        //srcDir.Delete(true);
                    }
     
     
                }
     
     
     
            }
     
     
     
     
        }
    }
    le piece jointe ne fonctionne pas sur la page du forum ,c'est louche !!!!
    bon code

Discussions similaires

  1. besoin d'aide pour projet
    Par feshida dans le forum C++Builder
    Réponses: 40
    Dernier message: 13/06/2007, 21h10
  2. Besoin d'idée pour projet réseau en C
    Par king_neo2001 dans le forum Réseau
    Réponses: 23
    Dernier message: 21/05/2007, 22h03
  3. [c#]besoin de sujet de projet
    Par dalhia dans le forum Général Dotnet
    Réponses: 4
    Dernier message: 11/05/2007, 23h30
  4. besoin d'aide pour projet
    Par donromanino dans le forum C++
    Réponses: 5
    Dernier message: 02/04/2006, 18h35

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo