2010-12-28 10:25:40Chris C.S Huang

[C#] 列示目錄下的檔案




程式碼如下:

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 Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            
            List<string> FileItems = new List<string>();

            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {                                          ///folderBrowserDialog選取目錄
                foreach (string File in Directory.GetFiles(folderBrowserDialog1.SelectedPath))
                {
                    FileItems.Add(File); //  
                }
            }
            listBox1.DataSource = FileItems;

        }
    }
}

參考資料:

Getting a list of all the files in a directory

C# ListBox in Windows Forms