.NET Framework 提供與 Windows 作業系統一致的標準對話方塊:OpenFileDialog、SaveFileDialog、PrintDialog、ColorDialog、FontDialog 等,繼承自 FileDialog 基類。
Filter:檔案類型過濾器,格式 "文字檔|*.txt|所有檔案|*.*"FilterIndex:預設選取的過濾器索引(1-based)ShowDialog():顯示對話方塊,回傳 DialogResult.OK 表示使用者按下確定FileName:使用者選取的完整路徑OpenFile():直接回傳 Stream 讀取檔案內容private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
using (StreamReader sr = new StreamReader(openFileDialog1.FileName))
{
MessageBox.Show(sr.ReadToEnd());
}
}
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "Cursor Files|*.cur";
dlg.Title = "Select a Cursor File";
if (dlg.ShowDialog() == DialogResult.OK && dlg.FileName != "")
{
this.Cursor = new Cursor(dlg.OpenFile());
}
}
FilterIndex 為 1-based,切勿誤用 0-based 索引。