OpenFileDialog 用于浏览并打开文件,在Windows Forms中使用,表现为标准的Windows对话框。

实例:

1.新建Windows Form Application

2.添加OpenFileDialog

打开Toolbox,找到并双击OpenFileDialog:

可以在窗口下方看到添加到OpenFileDialog.

3.添加按钮和事件

OpenFileDialog对话框需要通过事件激活,添加一个Button到Form中,双击Button以添加事件,事件代码如下:

using System;
using System.Windows.Forms; namespace OpenFileDialog {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
} private void button1_Click(object sender, EventArgs e) {
// show the dialog and get result
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
}
Console.WriteLine(result);
}
}
}

4.读取文件

可以通过OpenFileDialog读取文件,修改按钮的点击事件,具体代码如下:

using System;
using System.IO;
using System.Windows.Forms; namespace OpenFileDialog {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
int size = -1;
// show the dialog and get result
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
string file = openFileDialog1.FileName;
try
{
string text = File.ReadAllText(file);
size = text.Length;
}
catch (Exception)
{
throw;
}
}
Console.WriteLine(result);
Console.WriteLine(size); // 文件大小
}
}
}

编译运行,点击button1,会显示OpenFileDialog,然后验证DialogResult,通过File.ReadAllText读写文件,然后获得文件大小。

5. 属性

属性 说明
AddExtension 扩展名是否添加到文件名,默认为真,如果想自动修改文件扩展名,可将其设置为false。
AutoUpgradeEnabled 用于获得Vista风格的打开文件对话框,默认为true,推荐使用。
DefaultExt 默认文件扩展名,如果文件扩展名没有指定,则自动添加该扩展
DereferenceLinks 从对话框返回路径前是否解引用快捷键
FileName 在对话框最开始显式的文件名
InitialDirectory 对话框的初始目录
Multiselect 是否一次可选择多个文件,可通过SHIFT或CTRL键进行多项选择

6.Filter

Filters能帮助有效的筛选文件,OpenFileDialog支持文件名过滤,可用*代表任意字符。

Filter:

用于指定过滤器,如:“C# files|*.cs”,此时只显式以”.cs”结尾的文件

FilterIndex:

用于指定默认Filter,其Index为1.其后的filter的Index依次递增

ValidateNames:

Windows文件系统不允许文件名包含特定字符如”*”,该选项一般为True。

7.ReadOnly

OpenFileDialog部分属性可允许指定文件是否为只读。可以显式read-only checkbox。大多时候用不着.

ReadOnlyChecked:

用于设置”read only”复选框的值,仅当”ShowReadOnly”设置为True才可见。

ShowReadOnly:

“Read-only”复选框是否可见。

C# OpenFileDialog的更多相关文章

  1. 1、怎样设置C#OpenFileDialog(文件选择窗体)的指定路径、文件格式等属性(设置打开默认路径、文件格式、窗体显示文本)

    C#的OpenFileDialog的常用属性设置 1.设置属性 1)设置弹出的指定路径(绝对路径.相等路径) 2)设置标题 3)设置文本格式 2.打开方式1(绝对路径) 2.1) 打开的路径

  2. Dev 关于用openFileDialog控件上传图片的问题

    1. OpenFileDialog控件有以下基本属性 InitialDirectory 对话框的初始目录 Filter 要在对话框中显示的文件筛选器,例如,"文本文件(*.txt)|*.tx ...

  3. C# winform OpenFileDialog MessageBox

    1.弹出窗体选择本地文件-OpenFileDialog OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Tit ...

  4. OpenFileDialog获取文件名和文件路径问题

    OpenFileDialog获取文件名和文件路径问题(转) 转自:http://blog.sina.com.cn/s/blog_7511914e0101cbjn.html System.IO.Path ...

  5. 使用OpenFileDialog会更改默认程序目录

    这个问题可能只有在特定的程序中会发现:当我们在程序中使用相对路径时是依赖于当前目录的.所以在使用类似代码: XElement rootNode = XElement.Load(@"zips/ ...

  6. OpenFileDialog - 设置 - Filter 笔记

    using (OpenFileDialog fileSelectDialog = new OpenFileDialog()) { fileSelectDialog.Multiselect = fals ...

  7. C#的OpenFileDialog和SaveFileDialog的常见用法(转)

    OpenFileDialog openFileDialog1 = new OpenFileDialog();            openFileDialog1.InitialDirectory = ...

  8. OpenFileDialog使用方法

    OpenFileDialog基本属性 AddExtension 控制是否将扩展名自动添加到文件名上 CheckFileExists 指示用户指定不存在的文件时是否显示警告 CheckPathExist ...

  9. C# OpenFileDialog和PictrueBox

     string resultFile = ""; OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFile ...

随机推荐

  1. 【linux】who&&w

    who和w 看到目前服务器所登陆的用户,区别是w能看到更详细的信息. [root@andon tmp]# who admin tty5[tty指本地登陆] 2016-05-27 15:16[登陆时间] ...

  2. Centos7安装Zabbix3.0

    1.安装服务器端包 #rpm -ivh http://repo.zabbix.com/zabbix/3.0/rhel/7/x86_64/zabbix-release-3.0-1.el7.noarch. ...

  3. 在Myeclipse中移除项目对Hibernate的支持

    在Myeclipse中移除项目对Hibernate的支持 在使用Hibernate框架进行开发时可能会遇到配置错误或者需要删除Hibernate支持的情况.下面就说一下如何彻底移除项目的Hiberna ...

  4. ASP.NET的POST和GET提交并接收处理返回值

    POST方法: 数据提交 /// <summary> /// POST提交数据接收字符json /// </summary> /// <param name=" ...

  5. Linux下文件的压缩和解压

    tar命令 解包:tar zxvf FileName.tar 打包:tar czvf FileName.tar DirName gz命令 解压1:gunzip FileName.gz 解压2:gzip ...

  6. 242. Valid Anagram

    Given two strings s and t, write a function to determine if t is an anagram of s. For example,s = &q ...

  7. VS 开发工具中的Remote Debug 功能远程调试程序经验分享

    前言: 有时候我们Dev(开发人员)需要debug tester(测试人员)或者customer(客户)的环境,可tester的机器上没有Code,是不是有点着急? 而且是多版本应用且tester 发 ...

  8. PLSQL_性能优化系列14_Oracle High Water Level高水位分析

    2014-10-04 Created By BaoXinjian 一.摘要 PLSQL_性能优化系列14_Oracle High Water Level高水位分析 高水位线好比水库中储水的水位线,用于 ...

  9. DBA_Oralce Enterprise Manager OEM管理应用介绍(案例)

    2014-08-16 BaoXinjian

  10. CF534A Exam 构造

    An exam for n students will take place in a long and narrow room, so the students will sit in a line ...