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

Windows Forms Discussion :

[C#]Folder tree view et fichier/répertoire caché


Sujet :

Windows Forms

  1. #1
    Inscrit Avatar de bilb0t
    Profil pro
    Inscrit en
    Décembre 2003
    Messages
    378
    Détails du profil
    Informations personnelles :
    Âge : 46
    Localisation : Belgique

    Informations forums :
    Inscription : Décembre 2003
    Messages : 378
    Points : 283
    Points
    283
    Par défaut [C#]Folder tree view et fichier/répertoire caché
    Bonjour à tous,

    Ds un de mes projects, j'utilise la classe FolderTreeView

    Malheureusement, celle-ci n'affiche pas les fichiers/répertoires cachés. Quelqu'un connait t'il la solution pour les afficher ? Ou un autre controle qui list les fichier caché ?

    Merci.

  2. #2
    Expert éminent
    Avatar de neguib
    Profil pro
    Inscrit en
    Mai 2005
    Messages
    3 627
    Détails du profil
    Informations personnelles :
    Âge : 64
    Localisation : Suisse

    Informations forums :
    Inscription : Mai 2005
    Messages : 3 627
    Points : 7 879
    Points
    7 879
    Par défaut
    Mhhmm
    il me semble que de toute façon il te faudra implementer quelquechose de particulier autour des permissions pour avoir accès aux repertoires et fichiers masqués.

  3. #3
    Inscrit Avatar de bilb0t
    Profil pro
    Inscrit en
    Décembre 2003
    Messages
    378
    Détails du profil
    Informations personnelles :
    Âge : 46
    Localisation : Belgique

    Informations forums :
    Inscription : Décembre 2003
    Messages : 378
    Points : 283
    Points
    283
    Par défaut
    ok, je vais donc me rabattre sur un truc maison avec des directoryInfo...

    A moins que ça existe déja qqpart... ?

  4. #4
    Inscrit Avatar de bilb0t
    Profil pro
    Inscrit en
    Décembre 2003
    Messages
    378
    Détails du profil
    Informations personnelles :
    Âge : 46
    Localisation : Belgique

    Informations forums :
    Inscription : Décembre 2003
    Messages : 378
    Points : 283
    Points
    283
    Par défaut
    ok, j'en ai fait une. C'est vraiment pas très compliqué...

  5. #5
    Expert éminent
    Avatar de neguib
    Profil pro
    Inscrit en
    Mai 2005
    Messages
    3 627
    Détails du profil
    Informations personnelles :
    Âge : 64
    Localisation : Suisse

    Informations forums :
    Inscription : Mai 2005
    Messages : 3 627
    Points : 7 879
    Points
    7 879
    Par défaut
    Tu montres égoïste

  6. #6
    Inscrit Avatar de bilb0t
    Profil pro
    Inscrit en
    Décembre 2003
    Messages
    378
    Détails du profil
    Informations personnelles :
    Âge : 46
    Localisation : Belgique

    Informations forums :
    Inscription : Décembre 2003
    Messages : 378
    Points : 283
    Points
    283
    Par défaut
    Bon, c'est bien sur améliorable, et j'ai piquer la partie de code qui charge les icones (pour que ce soit plus joli), mais voilà le 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
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.IO;
    using System.Runtime.InteropServices;
     
    namespace Bilb0tSpace
    {
    	/// <summary>
    	/// Description résumée de DirTreeView.
    	/// </summary>
    	public class DirTreeView : TreeView
    	{
    		public DirTreeView():base()
    		{
    			this.ImageList = new ImageList();
    			//Event
    			this.AfterExpand+=new TreeViewEventHandler(DirTreeView_AfterExpand);
    		}
    		private static string TempNode = "TEMP_NODE";
     
    		public void AddRootNodes()
    		{
    			//foreach(string drive in Directory.GetLogicalDrives())
    			{
    				DirectoryInfo di =  new DirectoryInfo(/*drive*/"C:/");
    				if(di.Exists)
    				{
    					TreeNode tn = new TreeNode(/*drive*/"C:/");
     
    					ImageList.Images.Add(ExtractIcons.GetIcon(di.FullName,false,ImageList));
    					tn.ImageIndex = ImageList.Images.Count-1;
     
    					ImageList.Images.Add(ExtractIcons.GetIcon(di.FullName,true,ImageList));
    					tn.SelectedImageIndex = ImageList.Images.Count-1;
     
    					tn.Tag = di;
    					Nodes.Add(tn);
    					ExpandDir(tn);
    				}
    			}
    		}
     
    		public void ExpandDir(TreeNode tnParent)
    		{
    			DirectoryInfo di = (DirectoryInfo)tnParent.Tag;
    			if(di.Exists)
    			{
    				foreach(DirectoryInfo diChild in di.GetDirectories())
    				{
    					TreeNode tn = new TreeNode(diChild.Name);
    					tn.Tag = diChild;
     
    					ImageList.Images.Add(ExtractIcons.GetIcon(diChild.FullName,false,ImageList));
    					tn.ImageIndex = ImageList.Images.Count-1;
     
    					ImageList.Images.Add(ExtractIcons.GetIcon(diChild.FullName,true,ImageList));
    					tn.SelectedImageIndex = ImageList.Images.Count-1;
     
    					tnParent.Nodes.Add(tn);
    					if(diChild.GetDirectories().Length > 0)
    						tn.Nodes.Add(new TreeNode(TempNode));
    				}
    			}
    		}
     
    		private void DirTreeView_AfterExpand(object sender, TreeViewEventArgs e)
    		{
    			TreeNode tn = e.Node;
    			if(tn.Nodes.Count == 1)
    			{
    				if(tn.Nodes[0].Text == TempNode)
    				{
    					tn.Nodes.Clear();
    					ExpandDir(tn);
    				}
    			}
    		}
    	}
     
    	#region ExtractIcons Class
     
    	public class ExtractIcons
    	{
    		#region Structs & Enum
     
    		[StructLayout(LayoutKind.Sequential)]
    			private struct SHFILEINFO
    		{
    			public SHFILEINFO(bool b)
    			{
    				hIcon=IntPtr.Zero;iIcon=0;dwAttributes=0;szDisplayName="";szTypeName="";
    			}
    			public IntPtr hIcon;
    			public int iIcon;
    			public uint dwAttributes;
    			[MarshalAs(UnmanagedType.LPStr, SizeConst=260)]
    			public string szDisplayName;
    			[MarshalAs(UnmanagedType.LPStr, SizeConst=80)]
    			public string szTypeName;
    		};
     
    		private enum SHGFI
    		{
    			SHGFI_ICON =             0x000000100,     // get icon
    			SHGFI_DISPLAYNAME =      0x000000200,     // get display name
    			SHGFI_TYPENAME =         0x000000400,     // get type name
    			SHGFI_ATTRIBUTES =       0x000000800,     // get attributes
    			SHGFI_ICONLOCATION =     0x000001000,     // get icon location
    			SHGFI_EXETYPE =          0x000002000,     // return exe type
    			SHGFI_SYSICONINDEX =     0x000004000,     // get system icon index
    			SHGFI_LINKOVERLAY =      0x000008000,     // put a link overlay on icon
    			SHGFI_SELECTED =         0x000010000,     // show icon in selected state
    			SHGFI_ATTR_SPECIFIED =   0x000020000,     // get only specified attributes
    			SHGFI_LARGEICON =        0x000000000,     // get large icon
    			SHGFI_SMALLICON =        0x000000001,     // get small icon
    			SHGFI_OPENICON =         0x000000002,     // get open icon
    			SHGFI_SHELLICONSIZE =    0x000000004,     // get shell size icon
    			SHGFI_PIDL =             0x000000008,     // pszPath is a pidl
    			SHGFI_USEFILEATTRIBUTES = 0x000000010     // use passed dwFileAttribute
    		}
     
    		#endregion
     
    		#region Get Folder Icons
     
    		[DllImport("Shell32.dll")]
    		private static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, 
    			out SHFILEINFO psfi, uint cbfileInfo, SHGFI uFlags );
     
    		public static Icon GetIcon(string strPath, bool selected, ImageList imageList)
    		{
    			SHFILEINFO info = new SHFILEINFO(true);
    			int cbFileInfo = Marshal.SizeOf(info);
    			SHGFI flags;
    			if (!selected)
    				flags = SHGFI.SHGFI_ICON|SHGFI.SHGFI_SMALLICON;
    			else
    				flags = SHGFI.SHGFI_ICON|SHGFI.SHGFI_SMALLICON|SHGFI.SHGFI_OPENICON;
     
    			SHGetFileInfo(strPath, 256, out info,(uint)cbFileInfo, flags);
    			return Icon.FromHandle(info.hIcon);
    		}
     
    		#endregion
     
    		#region Get Desktop Icon
     
    		// Retreive the desktop icon from Shell32.dll - it always appears at index 34 in all shell32 versions.
    		// This is probably NOT the best way to retreive this icon, but it works - if you have a better way
    		// by all means let me know..
     
    		//		[DllImport("Shell32.dll", CharSet=CharSet.Auto)]
    		//		public static extern IntPtr ExtractIcon(int hInst, string lpszExeFileName, int nIconIndex);
    		//
    		//		public static Icon GetDesktopIcon()
    		//		{
    		//			IntPtr i = ExtractIcon(0, Environment.SystemDirectory + "\\shell32.dll", 34);
    		//			return Icon.FromHandle(i);
    		//		}
     
    		// Updated this method in v1.11 so that the icon returned is a small icon, not a large icon as
    		// returned by the old method above
     
    		[DllImport("Shell32.dll", CharSet=CharSet.Auto)]
    		public static extern uint ExtractIconEx(
    			string lpszFile, int nIconIndex, IntPtr[] phiconLarge, IntPtr[] phiconSmall, uint nIcons );
     
    		public static Icon GetDesktopIcon()
    		{
    			IntPtr[] handlesIconLarge = new IntPtr[1];
    			IntPtr[] handlesIconSmall = new IntPtr[1];
    			uint i = ExtractIconEx(Environment.SystemDirectory + "\\shell32.dll", 34, 
    				handlesIconLarge, handlesIconSmall, 1);
     
    			return Icon.FromHandle(handlesIconSmall[0]);
    		}
     
    		#endregion
     
    	}
     
    	#endregion
    }

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Selection d'un fichier dans le tree view
    Par penguin50 dans le forum NetBeans
    Réponses: 0
    Dernier message: 28/08/2008, 15h10
  2. Répertoire caché
    Par KUBITUS dans le forum Delphi
    Réponses: 30
    Dernier message: 13/04/2007, 07h19
  3. Comment savoir si un fichier/répertoire est en lecture seule
    Par Guigui_ dans le forum Général Python
    Réponses: 3
    Dernier message: 29/12/2004, 16h05
  4. Tree View avec XML et XSL
    Par Drooxy dans le forum XSL/XSLT/XPATH
    Réponses: 3
    Dernier message: 22/04/2003, 10h21
  5. Réponses: 5
    Dernier message: 06/03/2003, 13h27

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