C# 檔案對話方塊(FileDialog)

來源:互聯網
上載者:User

OpenFileDialog控制項有以下基本屬性

InitialDirectory 對話方塊的初始目錄
Filter 要在對話方塊中顯示的檔案篩選器,例如,"文字檔(*.txt)|*.txt|所有檔案(*.*)||*.*"
FilterIndex 在對話方塊中選擇的檔案篩選器的索引,如果選第一項就設為1
RestoreDirectory 控制對話方塊在關閉之前是否恢複目前的目錄
FileName 第一個在對話方塊中顯示的檔案或最後一個選取的檔案
Title 將顯示在對話方塊標題列中的字元
AddExtension 是否自動添加預設副檔名
CheckPathExists 在對話方塊返回之前,檢查指定路徑是否存在
DefaultExt 預設副檔名
DereferenceLinks 在從對話方塊返回前是否取值 (Dereference)捷徑
ShowHelp 啟用"協助"按鈕
ValiDateNames 控制對話方塊檢查檔案名稱中是否不含有無效的字元或序列

  2、 OpenFileDialog控制項有以下常用事件

FileOk 當使用者點擊"開啟"或"儲存"按鈕時要處理的事件
HelpRequest 當使用者點擊"協助"按鈕時要處理的事件

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace FileDialog
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private string fName;
        private bool isFileHaveName = false;
        private void 開啟SToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.InitialDirectory = "c:\\";//注意這裡寫路徑時要用c:\\而不是c:\
            openFileDialog.Filter = "文字檔|*.*|C#檔案|*.cs|所有檔案|*.*";
            openFileDialog.RestoreDirectory = true;
            openFileDialog.FilterIndex = 1;
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                
                fName = openFileDialog.FileName;
              
                File fileOpen = new File(fName);
                isFileHaveName = true;
                richTextBox1.Text = fileOpen.ReadFile();
                richTextBox1.AppendText("");
            }

        }
    }

 

 

///////////在應用於調用WMP控制項時////////////////////

openFileDialog1.Filter = "視頻檔案(*.avi;*.wmv;*.dat;*.mpg;*.mpeg;*.mov;*.wm;*.wma)|*.avi;*.wmv;*.dat;*.mpg;*.mpeg;*.mov;*.wm;*.wma|音頻檔案(*.wav;*.mp3;*.snd;*.au;*.midi;*.mid)|*.wav;*.mp3;*.snd;*.au;*.midi;*.mid|所有檔案(*.*)|*.*";        

//開啟的檔案類型 

if(openFileDialog1.ShowDialog()==DialogResult.OK) 

  axWindowsMediaPlayer1.FileName = openFileDialog1.FileName; 

//如果開啟的是音頻檔案,則禁止【視頻】菜單組 ,據此可判斷開啟的檔案類型

if(openFileDialog1.FilterIndex == 2) 

  menuItemVideo.Enabled = false; 

 else 

  menuItemVideo.Enabled = true; 

           

 }  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.