1. 程式人生 > >【C#】檔案選擇對話方塊OpenFileDialog與下列列表ComboBox

【C#】檔案選擇對話方塊OpenFileDialog與下列列表ComboBox

主要用如下的一個小例子,說明C#窗體中檔案選擇對話方塊OpenFileDialog與下列列表ComboBox的應用,點選“選擇檔案”按鈕,就可以開啟檔案選擇對話方塊OpenFileDialog,無論是多選檔案與單選檔案,都能夠將選擇的內容新增到下列列表ComboBox之中。同時,一開始,下列列表ComboBox是“無”的狀態,在檔案選擇對話方塊OpenFileDialog選擇的資料夾由於Windows本身的規定,會自動剔除。


基本佈局如下,如同《【C#】簡單窗體程式,判斷是否閏年,禁止窗體調整大小,關閉窗體前的判斷 》(點選開啟連結)利用工具欄上的對齊工具講各個元件擺好,同時將各個元件的Text改好,Form1的最大化按鈕去掉,FormBorderStyle改成FixedDialog,不可調整大小的狀態。下列列表ComboBox的DropDownStyle改成DropDownList,這種只選擇不輸入的狀態。


之後雙擊Combobox與Button為下列列表的改變與按鈕的點選新增事件,Form1具體程式碼如下。在Form1初始化的時候,先對Combobox進行初始化,加一個“無”的項上去,更加美觀。之後,點選按鈕,新建一個檔案選擇對話方塊OpenFileDialog,這個對話方塊預設是單選檔案狀態,我們要設定其為多選,如果選擇了檔案,先清空ComboBox,之後,被多選化的OpenFileDialog,會將開啟的檔案全路徑儲存到一個字串陣列OpenFileDialog.FileNames當中,第一個選擇的檔案路徑,則放到OpenFileDialog.FileName當中,如果你要再進一步操作檔案可以參考《【C#】利用System.IO中的File類中的ReadAllText與WriteAllText輸入輸出小檔案》(

點選開啟連結)。

最後,Combobox的變更與OpenFileDialog的選擇完成同樣,要獲取檔案的具體的檔名、副檔名,則要進一步對檔案路徑進行切割,OpenFileDialog的選擇完成還好,可以利用OpenFileDialog.SafeFileName;直接獲取選擇第一個檔案的檔名。而,Combobox則全程要利用Substring進行字串的切割,C#的Substring與Java不同,Java的Substring的字串是結束位置,C#則是取從第一個引數開始的位置之後的X個字元的意思。也沒什麼,注意一下就是。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;//使用的檔案分隔符這個常量const

namespace FileChoose
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;//設定Form窗體初始化放在螢幕中間  
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
            openFileDialog1.Multiselect = true;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)//判斷是否選擇了檔案
            {
                comboBox1.Items.Clear();//清除之前開啟的歷史
                foreach (string s in openFileDialog1.FileNames)
                {
                    comboBox1.Items.Add(s);
                }
                comboBox1.SelectedIndex = 0;
                string file_path = openFileDialog1.FileName;//記錄選擇的檔案全路徑
                string file_dir = file_path.Substring(0, file_path.LastIndexOf(Path.DirectorySeparatorChar) + 1);//獲取檔案目錄                  
                string file_name = openFileDialog1.SafeFileName;//獲取檔名
                string file_exc = file_name.Substring(file_name.LastIndexOf("."), file_name.Length - file_name.LastIndexOf(".")); //獲取副檔名       
                label1.Text = "檔案路徑:" + file_dir;//顯示檔案目錄
                label2.Text = "檔名稱:" + file_name;//顯示檔名
                label3.Text = "副檔名:" + file_exc;//顯示副檔名
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            comboBox1.Items.Add("無");
            comboBox1.SelectedIndex = 0;
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string file_path = comboBox1.Text;//雖然combobox1為只選擇不輸入的狀態,但是這樣就能夠直接獲取到combobox1的內容
            string file_dir = file_path.Substring(0, file_path.LastIndexOf(Path.DirectorySeparatorChar) + 1);//獲取檔案路徑
            string file_name = file_path.Substring(file_path.LastIndexOf(Path.DirectorySeparatorChar) + 1, file_path.Length - file_path.LastIndexOf(Path.DirectorySeparatorChar) - 1);
            string file_exc = file_name.Substring(file_name.LastIndexOf("."), file_name.Length - file_name.LastIndexOf(".")); //獲取副檔名       
            label1.Text = "檔案路徑:" + file_dir;//顯示檔案目錄
            label2.Text = "檔名稱:" + file_name;//顯示檔名
            label3.Text = "副檔名:" + file_exc;//顯示副檔名
        }
    }
}