OpenFileDialog控件有以下基本屬性
2、 OpenFileDialog控件有以下常用事件
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;
}