c#学习——OpenFileDialog文件对话框
2017-12-29 15:57
417 查看
下面是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文件下的代码如下
//获取或设置一个值,该值指示如果用户省略扩展名,
对话框是否自动在文件名中添加扩展名。
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# 打开文件对话框 OpenFileDialog
- 【C#】文件选择对话框OpenFileDialog与下列列表ComboBox
- C# OpenFileDialog 打开/选择文件对话框
- C# OpenFileDialog打开文件对话框(详解)
- 文件选择对话框filedialog,OpenFileDialog,FolderBrowserDialog
- c#:文件对话框(FileDialog)
- C#用openfiledialog文件和savefileDialog打开和保存文件
- c#:文件对话框(FileDialog)
- 4.20学习笔记c# OpenFileDialog
- C#中OpenFileDialog获取文件名和文件路径的常用方法
- C#对话框(OpenFileDialog、SaveFileDialog 、ColorDialog、FontDialog)
- SAP ABAP编程 选择文件对话框 CL_GUI_FRONTEND_SERVICES=>FILE_OPEN_DIALOG
- C# 自定义OpenFileDialog屏蔽一些功能,缩略图显示文件夹,文件等等
- Creating Dialogbased Windows Application (4) / 创建基于对话框的Windows应用程序(四)Edit Control、Combo Box的应用、Unicode转ANSI、Open File Dialog、文件读取、可变参数、文本框自动滚动 / VC++, Windows
- 如何找到文件的家-打开文件对话框openFileDialog
- c#:文件对话框(FileDialog)
- c#openFileDialog读取文件
- (转帖)使用OpenFileDialog对话框打开文本文件(写此代码的人应该是有一定功力的程序员,因为代码写的很规范,很容易阅读,很好理解,很好调试)
- C#中OpenFileDialog获取文件名和文件路径的常用方法.
- C# FolderBrowserDialog和OpenFileDialog 使用 指定目录和指定文件