c#学习——OpenFileDialog文件对话框

下面是OpenFileDialog类的属性!!!!!!!!!!!!!!

//获取或设置一个值,该值指示如果用户省略扩展名,
对话框是否自动在文件名中添加扩展名。
dlg.AddExtension = cbAddExtension.Checked;
//获取或设置一个值,该值指示如果用户指定不存在的文件名
对话框是否显示警告
dlg.CheckFileExists = cbCheckFileExists.Checked;
//获取或设置一个值,该值指示如果用户指定不存在的路径,
对话框是否显示警告
dlg.CheckPathExists = cbCkeckPathExists.Checked;
//获取或设置一个值,该值指示对话框是否允许选择多个文件
dlg.Multiselect = cbMltiSelect.Checked;
//获取或设置一个值,该值指示是否选定只读复选框
dlg.ReadOnlyChecked = cbReadOnlyChecked.Checked;
//获取或设置一个值,该值指示该对话框在关闭前是否将目录还原为之前选定的目录

dlg.RestoreDirectory = cbRestoreDirectory.Checked;
//该值指示是否帮助按钮显示在文件对话框中
dlg.ShowHelp = cbShowHelp.Checked;
//
dlg.ShowReadOnly = cbShowReadOnly.Checked;
//该值指示对话框是否只接受有效的win32文件名
dlg.ValidateNames = cbValidateNames.Checked;
//获取或设置默认文件扩展名
dlg.DefaultExt = txtDefaultExt.Text;
//获取或设置一个包含在文件对话框中选定的文件名的字符串
dlg.FileName = txtFileNames.Text;
//获取或设置当前文件名筛选器字符串,该字符串决定对话框的“另存为文件类型”或“文件类型”框中出现的选择内容
dlg.Filter = txtFilter.Text;
if ((txtFilterIndex.Text.Trim()).Length < 1)
dlg.FilterIndex = 1;
else
dlg.FilterIndex = int.Parse(txtFilterIndex.Text);
dlg.InitialDirectory = txtInirialDirecory.Text;
dlg.Title = txtTitle.Text;
if (dlg.ShowDialog() == DialogResult.OK)
{
txtFileName.Text = dlg.FileName;
txtFileNames.Lines = dlg.FileNames;
}

把主窗体的标题改为“测试文件对话框”,然后向主窗体中添加图片所示附件
这里写图片描述

这里写图片描述

Form1.cs文件下的代码如下

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;

namespace FileTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnOpenFileDialog_Click(object sender, EventArgs e)
        {
            //创建对话框实例
            OpenFileDialog dlg = new OpenFileDialog();
            //设置对话框的属性
            dlg.AddExtension = cbAddExtension.Checked;
            dlg.CheckFileExists = cbCheckFileExists.Checked;
            dlg.CheckPathExists = cbCkeckPathExists.Checked;
            dlg.Multiselect = cbMltiSelect.Checked;
            dlg.ReadOnlyChecked = cbReadOnlyChecked.Checked;
            dlg.RestoreDirectory = cbRestoreDirectory.Checked;
            dlg.ShowHelp = cbShowHelp.Checked;
            dlg.ShowReadOnly = cbShowReadOnly.Checked;
            dlg.ValidateNames = cbValidateNames.Checked;
            dlg.DefaultExt = txtDefaultExt.Text;
            dlg.FileName = txtFileNames.Text;
            dlg.Filter = txtFilter.Text;
            if ((txtFilterIndex.Text.Trim()).Length < 1)
                dlg.FilterIndex = 1;
            else
                dlg.FilterIndex = int.Parse(txtFilterIndex.Text);
            dlg.InitialDirectory = txtInirialDirecory.Text;
            dlg.Title = txtTitle.Text;
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                txtFileName.Text = dlg.FileName;
                txtFileNames.Lines = dlg.FileNames;
            }
        }

        private void btnSaveFileDialog_Click(object sender, EventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.AddExtension = cbAddExtension.Checked;
            dlg.CheckFileExists = cbCheckFileExists.Checked;
            dlg.CheckPathExists = cbCkeckPathExists.Checked;


            dlg.RestoreDirectory = cbRestoreDirectory.Checked;
            dlg.ShowHelp = cbShowHelp.Checked;

            dlg.ValidateNames = cbValidateNames.Checked;
            dlg.CreatePrompt = cbCreatPrompt.Checked;
            dlg.OverwritePrompt = cbOverwritePrompt.Checked;

            dlg.DefaultExt = txtDefaultExt.Text;
            dlg.FileName = txtFileNames.Text;
            dlg.Filter = txtFilter.Text;
            if ((txtFilterIndex.Text.Trim()).Length < 1)
                dlg.FilterIndex = 1;
            else
                dlg.FilterIndex = int.Parse(txtFilterIndex.Text);
            dlg.InitialDirectory = txtInirialDirecory.Text;
            dlg.Title = txtTitle.Text;
            if (dlg.ShowDialog() == DialogResult.OK)
                txtFileName.Text = dlg.FileName;


        }
    }
}

这里写图片描述

你可能感兴趣的:(c#,对话框,c#)