Lister un dossier et ses fichiers

Description

Ca permet de lister les fichiers et les répertoires que contient le dossier sélectionné.

Source / Exemple :


using System;
using System.Windows.Forms;
using System.IO;

namespace DefaultNamespace
{
	/// <summary>
	/// Description of MainForm.	
	/// </summary>
	public class MainForm : System.Windows.Forms.Form
	{
		private System.Windows.Forms.MenuItem lister;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.MenuItem about;
		private System.Windows.Forms.MenuItem quitter;
		private System.Windows.Forms.FolderBrowserDialog browsedialog;
		private System.Windows.Forms.MenuItem efface;
		private System.Windows.Forms.ListBox listBoxDossier;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.TextBox nomfichier;
		private System.Windows.Forms.Button Effacer;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.ListBox listBoxFichier;
		private System.Windows.Forms.MenuItem menuItem2;
		private System.Windows.Forms.MenuItem menuItem1;
		private System.Windows.Forms.MainMenu mainMenu1;
		public MainForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
		}
		
		[STAThread]
		public static void Main(string[] args)
		{
			Application.Run(new MainForm());
		}
		
		#region Windows Forms Designer generated code
		/// <summary>
		/// This method is required for Windows Forms designer support.
		/// Do not change the method contents inside the source code editor. The Forms designer might
		/// not be able to load this method if it was changed manually.
		/// </summary>
		private void InitializeComponent() {
			this.mainMenu1 = new System.Windows.Forms.MainMenu();
			this.menuItem1 = new System.Windows.Forms.MenuItem();
			this.menuItem2 = new System.Windows.Forms.MenuItem();
			this.listBoxFichier = new System.Windows.Forms.ListBox();
			this.button1 = new System.Windows.Forms.Button();
			this.Effacer = new System.Windows.Forms.Button();
			this.nomfichier = new System.Windows.Forms.TextBox();
			this.label1 = new System.Windows.Forms.Label();
			this.listBoxDossier = new System.Windows.Forms.ListBox();
			this.efface = new System.Windows.Forms.MenuItem();
			this.browsedialog = new System.Windows.Forms.FolderBrowserDialog();
			this.quitter = new System.Windows.Forms.MenuItem();
			this.about = new System.Windows.Forms.MenuItem();
			this.label2 = new System.Windows.Forms.Label();
			this.lister = new System.Windows.Forms.MenuItem();
			this.SuspendLayout();
			// 
			// mainMenu1
			// 
			this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
						this.menuItem1,
						this.menuItem2});
			// 
			// menuItem1
			// 
			this.menuItem1.Index = 0;
			this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
						this.lister,
						this.efface,
						this.quitter});
			this.menuItem1.Text = "Fichier";
			// 
			// menuItem2
			// 
			this.menuItem2.Index = 1;
			this.menuItem2.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
						this.about});
			this.menuItem2.Text = "?";
			// 
			// listBoxFichier
			// 
			this.listBoxFichier.Location = new System.Drawing.Point(296, 64);
			this.listBoxFichier.Name = "listBoxFichier";
			this.listBoxFichier.Size = new System.Drawing.Size(248, 225);
			this.listBoxFichier.TabIndex = 0;
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(448, 8);
			this.button1.Name = "button1";
			this.button1.TabIndex = 1;
			this.button1.Text = "Lister";
			this.button1.Click += new System.EventHandler(this.Button1Click);
			// 
			// Effacer
			// 
			this.Effacer.Location = new System.Drawing.Point(448, 32);
			this.Effacer.Name = "Effacer";
			this.Effacer.TabIndex = 3;
			this.Effacer.Text = "Effacer";
			this.Effacer.Click += new System.EventHandler(this.EffacerClick);
			// 
			// nomfichier
			// 
			this.nomfichier.Location = new System.Drawing.Point(32, 8);
			this.nomfichier.Name = "nomfichier";
			this.nomfichier.Size = new System.Drawing.Size(408, 20);
			this.nomfichier.TabIndex = 4;
			this.nomfichier.Text = "";
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(32, 48);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(96, 16);
			this.label1.TabIndex = 5;
			this.label1.Text = "Dossiers";
			// 
			// listBoxDossier
			// 
			this.listBoxDossier.Location = new System.Drawing.Point(32, 64);
			this.listBoxDossier.Name = "listBoxDossier";
			this.listBoxDossier.Size = new System.Drawing.Size(248, 225);
			this.listBoxDossier.TabIndex = 2;
			// 
			// efface
			// 
			this.efface.Index = 1;
			this.efface.Text = "Effacer";
			this.efface.Click += new System.EventHandler(this.EffacerClick);
			// 
			// browsedialog
			// 
			this.browsedialog.Description = "Sélectionner un dossier";
			this.browsedialog.ShowNewFolderButton = false;
			// 
			// quitter
			// 
			this.quitter.Index = 2;
			this.quitter.Shortcut = System.Windows.Forms.Shortcut.AltF4;
			this.quitter.Text = "Quitter";
			this.quitter.Click += new System.EventHandler(this.quitterClick);
			// 
			// about
			// 
			this.about.Index = 0;
			this.about.Shortcut = System.Windows.Forms.Shortcut.F1;
			this.about.Text = "A propos";
			this.about.Click += new System.EventHandler(this.aboutClick);
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(296, 48);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(96, 16);
			this.label2.TabIndex = 6;
			this.label2.Text = "Fichiers";
			// 
			// lister
			// 
			this.lister.Index = 0;
			this.lister.Shortcut = System.Windows.Forms.Shortcut.CtrlO;
			this.lister.Text = "Lister";
			this.lister.Click += new System.EventHandler(this.Button1Click);
			// 
			// MainForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(560, 294);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.nomfichier);
			this.Controls.Add(this.Effacer);
			this.Controls.Add(this.listBoxDossier);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.listBoxFichier);
			this.Menu = this.mainMenu1;
			this.Name = "MainForm";
			this.Text = "Lister fichiers et répertoires";
			this.ResumeLayout(false);
		}
		#endregion
		void Button1Click(object sender, System.EventArgs e)
		{
			listBoxFichier.Items.Clear();
			listBoxDossier.Items.Clear();
			
			
			if (browsedialog.ShowDialog()==DialogResult.OK)
			{	
			nomfichier.Text=browsedialog.SelectedPath;
			
			String[] fichiers = Directory.GetFiles(nomfichier.Text);
        	for (int i = 0 ; i < fichiers.Length ; i++)
        	{
            	listBoxFichier.Items.Add(fichiers[i]);
        	}
        	
        	String[] reps = Directory.GetDirectories(nomfichier.Text);
        
        	for (int i = 0 ; i < reps.Length ; i++)
			{
            	listBoxDossier.Items.Add(reps[i]);
        	}
			}
		}
		void EffacerClick(object sender, System.EventArgs e)
		{
			listBoxFichier.Items.Clear();
			listBoxDossier.Items.Clear();
			nomfichier.Clear();
		}
	
		void aboutClick(object sender, System.EventArgs e)
		{
			MessageBox.Show("             © Tahiti_bob    \n  http://cginvent.virtuelnet.net      ", "A propos", MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1);
		}
	
		void quitterClick(object sender, System.EventArgs e)
		{
			Application.Exit();
		}
	
	
	}
}

Conclusion :


Bug:
Résolu ------>Lorsque l'on fait "Annuler" dans le FolderBrowserDialog, on a une :
>"Unhandled exception has occured in your application..."
>Si qqn peut m'aider...

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.